android timestamp parsing gone wrong(always in 1970)

10,686

Solution 1

You are using the wrong format string in the first line:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");

mm is minutes. Use MM (months) instead.

edit A Unix timestamp is a number of seconds since 01-01-1970 00:00:00 GMT. Java measures time in milliseconds since 01-01-1970 00:00:00 GMT. You need to multiply the Unix timestamp by 1000:

cal.setTimeInMillis(dateMulti * 1000L);

Solution 2

Why you have "dd-mm-yyyy" in SimpleDateFormat and "dd-MM-yyyy" in DateFormat.format? Use this :

String date = DateFormat.format("dd-mm-yyyy", cal).toString();

If you want minutes, if you want months you have to put MM like @Jesper said :)

Solution 3

I should like to contribute the modern answer.

java.time

    DateTimeFormatter dateFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(Locale.forLanguageTag("da"));
    String unixTimeStampString = "1427101853";
    int dateMulti = Integer.parseInt(unixTimeStampString);
    ZonedDateTime dateTime = Instant.ofEpochSecond(dateMulti)
            .atZone(ZoneId.of("Africa/Conakry"));
    String formattedDate = dateTime.format(dateFormatter);
    System.out.println(formattedDate);

The output from this snippet is:

23-03-2015

The output agrees with an online converter (link at the bottom). It tells me your timestamp equals “03/23/2015 @ 9:10am (UTC)” (it also agrees with the date you asked the question). Please substitute your time zone if it didn’t happen to be Africa/Conakry.

The date-time classes that you were using — SimpleDateFormat, Date and Calendar — are long outdated and poorly designed, so I suggest you skip them and use java.time, the modern Java date and time API, instead. A minor one among the many advantages is it accepts seconds since the epoch directly, so you don’t need to convert to milliseconds. While this was no big deal, doing your own time conversions is a bad habit, you get clearer, more convincing and less error-prone code from leaving the conversions to the appropriate library methods.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

I wrote and ran the above snippet using the backport to make sure it would be compatible with ThreeTenABP.

Links

Share:
10,686
Twizzler
Author by

Twizzler

Im Bart from the Netherlands I used to do Application development finished it and now im doing Advance sensor applications. I love to make new things and discover new languages and stuff!

Updated on July 28, 2022

Comments

  • Twizzler
    Twizzler almost 2 years

    im trying to convert a string(with unix timestamp) to an date with the format ( dd-MM-yyyy)

    and this is working partly. The problem im having now is that my date is in 17-01-1970 (instead of march 16 2015)

    im converting it like this:

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date d = null;
        int dateMulti = Integer.parseInt(Date);
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(dateMulti);
        String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    
        Log.d("test",date);
        try {
            d = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    

    where Date = 1427101853 and the result = 17-01-1970

    what am i doing wrong?

  • Twizzler
    Twizzler about 9 years
    changed it to dd-MM-yyyy there is a change the outcome is the same
  • Jesper
    Jesper about 9 years
    Unix timestamp is in seconds. You need to multiply by 1000 to get milliseconds: cal.setTimeInMillis(dateMulti * 1000L);
  • Twizzler
    Twizzler about 9 years
    then i get the minuts i need the months that was a mistake by me but your input was great!
  • Abdulmoiz Ahmer
    Abdulmoiz Ahmer about 2 years
    Add explanation to your snippet.
  • Dev4Life
    Dev4Life almost 2 years
    That 1000L trick did the job! Thanks @Jesper.