简体   繁体   中英

Date in a format

I have this code

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'-'hh:mm")
DateTime dateTime1 = new DateTime()
println("dateTime1 : " + dateTime1)
DateTime formattedDate = fmt.parseDateTime(dateTime1.toString());
println("formattedDate : " + formattedDate)
DateTimeFormatter finalFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm'-'hh:mm");
System.out.println("formatted date : " + finalFormat.print(formattedDate));

It prints out something like this :

dateTime1 : 2014-08-20T15:34:17.256-04:00
formattedDate : 2014-08-20T16:00:17.256-04:00
formatted date : 2014-08-20T16:00-04:00

I want a date in exactly this format(since those are the requirements)

2014-08-20T15:34-04:00

The problem with

formattedDate : 2014-08-20T16:00:17.256-04:00
formatted date : 2014-08-20T16:00-04:00

is for time difference it always prints out '16:00-04:00'

How do I get that ? The date is retrieved from the database which will be converted in to DateTime object.

When I try this :

    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    DateTime dateTime1 = new DateTime()
    println("dateTime1 : " + dateTime1)
    DateTime formattedDate = fmt.parseDateTime(dateTime1.toString());
    println("formattedDate : " + formattedDate)
    DateTimeFormatter finalFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm'-'hh:mm");
    System.out.println("formatted date : " + finalFormat.print(formattedDate));

the output changes :

    dateTime1 : 2014-08-20T15:59:34.876-04:00
    formattedDate : 2014-08-20T15:59:34.876-04:00
    formatted date : 2014-08-20T15:59-03:59

It should be -04.00. But comes back as '-03:59', which is buggy.

Any help is highly appreciated.

Thanks

The problem is in your pattern

"yyyy-MM-dd'T'HH:mm'-'hh:mm"

When you add 'hh:mm' you are reading the time again as 04:00, however since it has read that it is a PM time already, this becomes 16:00. You should read a timezone as a timezeone with a Z , not as part of the time itself.

From the Javadoc for DateTimeFormat

Symbol  Meaning              Presentation  Examples  
------  -------              ------------  -------
Z       time zone offset/id  zone          -0800; -08:00; America/Los_Angeles

and

Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.

I suggest you use

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")

Note: the - at the end is not a separator, but a minus sign. I am guessing the -04:00 timezone is the AEST timezone.

Your time format is incorrect, more specifically the last part makes it parse the timezone as the time of day (hours and minutes). Try using this instead:

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")

Your offset format is incorrect ZZ is timezone with colon, and you need to add withOffsetParsed() -

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mmZZ");
String dateTime1 = "2014-08-20T15:34-04:00";
DateTime formattedDate = fmt.withOffsetParsed().parseDateTime(dateTime1);
System.out.println("formattedDate : " + formattedDate);
System.out.println("formatted date : " + fmt.print(formattedDate));

Output is

formattedDate : 2014-08-20T15:34:00.000-04:00
formatted date : 2014-08-20T15:34-04:00

From the DateTimeFormat javadoc ,

Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.

The other answers are correct.

But you could more simply use a built-in formatter to produce a format of yyyy-MM-dd'T'HH:mm .

DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute().withZone( DateTimeZone.UTC ) ; 
String output = formatter.print( DateTime.now() ) + "Z";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM