Joda Time subtracts 24 hours from an instance of MutableDateTime, I would like to know why

I don’t understand why it MutableDateTime.setDate()sets the time to “yesterday” (see the timestamp hours of the journal - this is 20:28). Is this time zone related? Do I need to install something in formatting?

I would expect that after calling setDate from "10/27/2010" the date would be the same as the processed date 00:00 EDT 10/27/10, instead of 20:28 EDT 10/26/10, This is 24 hours ago from "now."

What am I missing here, or how can I change the code to get the desired result? I am new to Joda Time and would like to solve this mystery.

DateTimeFormatter dateFormatterJ = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTimeFormatter timestampFormatJ = DateTimeFormat.forPattern("HH:mm zzz MM/dd/yy");

MutableDateTime startDate = new MutableDateTime();

log.info("parsed date " + 
    timestampFormatJ.print(dateFormatterJ.parseMutableDateTime(startDateString)));

startDate.setDate((dateFormatterJ.parseMutableDateTime(startDateString)));

log.info("startDate: " + timestampFormatJ.print(startDate));

In this case, startDateStringsimply "10/27/2010".

here is the log output:

10-27 20:28:55 INFO parsed date: 00:00 EDT 10/27/10
10-27 20:28:55 INFO startDate: 20:28 EDT 10/26/10

thank

+5
2

, GMT , :

  • ,
  • withZone()
  • parseLocalDate() parseMutableDateTime()

, , , , .

parseLocalDate() .

+2

, javadoc .

public void setDate (ReadableInstant )

. .

: - ,

: IllegalArgumentException - invalidobject

"", "". " ", java.util.Date. ( , , , .)

EDIT: " ", :

MutableDateTime startDate = new MutableDateTime(dateFormatterJ.parseMutableDateTime(startDateString));

.

2: , , -, , . .

3: , , , .

public void setDate(final ReadableInstant instant) {
    long instantMillis = DateTimeUtils.getInstantMillis(instant);
    Chronology instantChrono = DateTimeUtils.getInstantChronology(instant);
    DateTimeZone zone = instantChrono.getZone();
    if (zone != null) {
        instantMillis = zone.getMillisKeepLocal(**DateTimeZone.UTC**, instantMillis);
    }
    setDate(instantMillis);
}

- UTC . , 10/27/2010 00:00 EDT, , 10/27/2010 00:00 UTC, , , 6 7 , EDT, 10/26.

, - , 2 .)

+4

All Articles