android timestamp parsing gone wrong(always in 1970)

后端 未结 4 1924
夕颜
夕颜 2020-12-16 07:08

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

相关标签:
4条回答
  • 2020-12-16 07:30

    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);
    
    0 讨论(0)
  • 2020-12-16 07:43

    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 :)

    0 讨论(0)
  • 2020-12-16 07:45

    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

    • Timestamp Converter
    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
  • 2020-12-16 07:45

    I was also facing the same issue when I was using SimpleDateFormat Here is a method I have made, which is working fine for me.

     private String getmDate(long time1) {
        java.util.Date time = new java.util.Date((long) time1 * 1000);
        String date = DateFormat.format("dd-MMM-yyyy' at 'HH:mm a", time).toString();
        return date + "";
    }
    

    you can change the date format as you desire.

    0 讨论(0)
提交回复
热议问题