SimpleDateFormat with TimeZone

前端 未结 6 800
旧巷少年郎
旧巷少年郎 2020-12-06 00:56

I\'m trying to format date from java.util.Date. I need this format:

2016-06-10T13:38:13.687+02:00.

How correctly convert this from standard Da

相关标签:
6条回答
  • 2020-12-06 01:37

    Just turn your z to upperCase

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.getDefault());
        sdf.format(new Date());
    

    Result: 2016-06-10T13:53:22 +0200

    0 讨论(0)
  • 2020-12-06 01:38

    You just made a simple mistake, You need to use a capital z. What You need is:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z Z", Locale.getDefault());
    

    in addition to your lower case z. A lower case z gives you just the time zone, but the capital one gives you the time zone based on RFC 822.

    EDIT

    If you not want a usual time zone, only need +2:00 without for example PST, you only need a capital Z:

       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.getDefault());
    

    From the (very simple understandable) Docs:

    z/zz/zzz:PST zzzz:Pacific Standard Time
    Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
    
    0 讨论(0)
  • 2020-12-06 01:39

    As per the standard Java docs: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

    for getting date and time formatting of

    2001-07-04T12:08:56.235-07:00


    You Need to use below String pattern:

    "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"

    So with below code, you can get what you want:

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault());
        simpleDateFormat .format(new Date());
    
    0 讨论(0)
  • 2020-12-06 01:39

    Add SSSZ in the format

    "yyyy-MM-dd'T'HH:mm:ss z" => "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

    Just change this line

    Old: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault()); sdf.format(new Date());

    New: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());

    0 讨论(0)
  • 2020-12-06 01:44

    You have add (ZZZZZ) at the end to get this format like below

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss ZZZZZ", Locale.getDefault());
    
    0 讨论(0)
  • 2020-12-06 01:57

    Time for someone to provide the modern answer.

        OffsetDateTime now = OffsetDateTime.now(ZoneId.of("Africa/Cairo"));
        String formatted = now.toString();
        System.out.println(formatted);
    

    This just printed

    2018-02-28T16:42:59.628526+02:00

    Edit: I asked for 3 decimals on the seconds, not 6. To exercise more control over the format, use an explicit formatter:

        DateTimeFormatter formatterWithThreeDecimals
                = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX");
        String formatted = now.format(formatterWithThreeDecimals);
        System.out.println(formatted);
    

    This printed (at time of my edit):

    2018-04-30T15:47:41.749+02:00

    Please substitute your desired time zone if it didn’t happen to be Africa/Cairo. Specify ZoneId.systemDefault() for your JVM’s time zone setting. The setting can be changed any time by another part of your program or another program running in the same JVM.

    Using java.time, the modern Java date and time API, in case you just want an utput that conforms with ISO 8601 and don’t care how many decimals (or whether seconds are printed if they are 0), we don’t need an explicit formatter at all. Your desired string is in ISO 8601 format, the standard format that the modern classes’ toString methods produce. Depending on what you need it for, the result of toString will probably accepted.

    The classes you were using, Date and SimpleDateFormat, are long outdated, and the latter in particular notoriously troublesome. I recommend you avoid them and use the modern API instead. It is so much nicer to work with.

    If you got a java.util.Date from a legacy API that you don’t want to change just now, convert it to a modern Instant and do further conversions from there. In Java 8 and later this happens like this:

        ZonedDateTime dateTime = oldfashionedDateFromLegacyApi.toInstant()
                .atZone(ZoneId.of("Africa/Cairo"));
        String formatted = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    

    If you are using ThreeTen Backport and/or ThreeTenABP, the conversion happens a little differently, like this:

        ZonedDateTime dateTime = DateTimeUtils.toInstant(oldfashionedDateFromLegacyApi)
                .atZone(ZoneId.of("Africa/Cairo"));
    

    Again, if you want close control over the output format, don’t use DateTimeFormatter.ISO_OFFSET_DATE_TIME since it will output anything from 1 through 9 decimals and may also leave out the seconds if they are 0. Instead use my formatterWithThreeDecimals from above.

    Question: Can I use java.time on Android?

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

    • In Java 8 and later and on newer Android devices 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.

    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.timeto 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)
提交回复
热议问题