1

"dt":1427700245 How I change this date format into 17-Mar-2017 13:40

String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date(weatherResponse.getDt()*1000));
            date.setText(currentDateTimeString);

It shows me 25 Jan 1969 01:52:28

Iwo Kucharski
  • 3,735
  • 3
  • 50
  • 66
Prem_Soren
  • 109
  • 3
  • 14
  • I strongly recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 19 '22 at 09:24
  • I cannot reproduce. On my Java `new Date(1427700245 * 1000)` gave `Wed Jan 21 12:58:22 CET 1970`. Also `17-Mar-2017 13:40` would correspond to something around `1489671600` seconds since the epoch depending on time zone. Certainly not 1427700245. – Ole V.V. Jun 19 '22 at 09:24
  • `Instant.ofEpochSecond(1_489_738_200L).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT))`. Gave `17/03/17, 1:40 PM`. Excat output will depend on time zone and locale. – Ole V.V. Jun 19 '22 at 10:20

3 Answers3

1

The openweather API documentation states that the date format is in Unix UTC format.

https://openweathermap.org/api/one-call-3

Here's an answer on SO that describes how to convert the Unix timestamp date:

Convert unix timestamp to date in java

so your date 1427700245 is 2015-03-30 03:24:05 (GMT-04:00).

    long unixSeconds = 1427700245;
    // convert seconds to milliseconds
    Date date = new java.util.Date(unixSeconds*1000L); 
    
    // the format of your date
    SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
    // give a timezone reference for formatting (see comment at the bottom)
    sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4")); 
    String formattedDate = sdf.format(date);
    System.out.println(formattedDate);

EDIT: using DateTimeFormatter which is also included in the above mentioned SO post.

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        final long unixTime = 1427700245;
        final String formattedDtm = Instant.ofEpochSecond(unixTime)
                .atZone(ZoneId.of("GMT-4"))
                .format(formatter);

        System.out.println(formattedDtm);   // => 2015-03-30 03:24:05
BustedSanta
  • 1,368
  • 7
  • 28
  • 55
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table). – Ole V.V. Jun 19 '22 at 09:17
  • 1
    @Ole V.V. - thank you for your feedback... the alternative solution using DateTimeFormatter has been added to the answer – BustedSanta Jun 19 '22 at 12:28
  • Using `DateTimeFormatter`, `Instant` and `ZoneId` is strongly encouraged and recommended. – Ole V.V. Jun 19 '22 at 13:19
-1

Its precision issue. Explicitly define you date value as long. It will work then

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

rohitanand
  • 710
  • 1
  • 4
  • 26
-1

Try this

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy'T'HH:mm"):
String dateString = sdf.format(weatherResponse.getDt()*1000):
date.setText(dateString):
albeee
  • 1,452
  • 1
  • 12
  • 20