Change date string format in android

后端 未结 6 743
忘了有多久
忘了有多久 2020-12-01 04:14

I am getting date string from SAX parsing like this: Wed, 18 Apr 2012 07:55:29 +0000

Now, I want this string as : Apr 18, 2012 01:25 PM

6条回答
  •  鱼传尺愫
    2020-12-01 04:47

    java.time and ThreeTenABP

    The modern answer to this question is overdue. When this question was asked in 2012, using SimpleDateFormat and/or DateFormat as the other answers do, was right even though those classes had always been troublesome. They were replaced just a couple of years later by java.time, the modern Java date and time API, which I frankly find much nicer to work with. So so I am doing.

    I suggest you separate your conversion into two operations. In you program don’t keep date and time as a string. Keep them as a proper date-time object. So when parsing from SAX also parse the date-time string into an OffsetDateTime immediately. When at a later point you need to show the date and time to the user (or transmit it to another system), convert it into the user’s time zone and format it into an appropriate string for that purpose.

    Parse into an OffsetDateTime

    The string you got conforms with RFC 1123 format. java.time has a built-in formatter for that, which is good because it saves us from constructing our own formatter.

        String stringFromSaxParsing = "Wed, 18 Apr 2012 07:55:29 +0000";
    
        OffsetDateTime dateTime = OffsetDateTime.parse(
                stringFromSaxParsing, DateTimeFormatter.RFC_1123_DATE_TIME);
    
        System.out.println(dateTime);
    

    Output from this snippet is:

    2012-04-18T07:55:29Z

    Convert to user’s time zone and format

        DateTimeFormatter wantedFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu hh:mm a", Locale.ENGLISH);
    
        String formatted = dateTime.atZoneSameInstant(ZoneId.systemDefault())
                .format(wantedFormatter);
    
        System.out.println(formatted);
    

    I ran this in Asia/Colombo time zone and got the desired:

    Apr 18, 2012 01:25 PM

    Question: Doesn’t java.time require Android API level 26?

    java.time works nicely on both 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) the modern API comes built-in.
    • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern 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.

    Links

    • 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.

提交回复
热议问题