SimpleDateFormat with TimeZone

前端 未结 6 915
旧巷少年郎
旧巷少年郎 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: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
    

提交回复
热议问题