无法在 Java 中正确解析 00:00:00 和 23:59:59 EST 的日期

Could not parse Date correctly for 00:00:00 and 23:59:59 EST in Java

我试图将 startDate 的时间传递为​​ 00:00:00 并将 endDate 的时间传递为​​ 23:59:59 但在调试期间startDate 的时间是 Thu Aug 09 10:30:00 IST 2018,endDate 的时间是 Tue Aug 14 10:29:59 IST 2018。我在哪里我做错了吗?

SimpleDateFormat estFormat=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
estFormat.setTimeZone(TimeZone.getTimeZone("EST"));

Date startDate=estFormat.parse(sdate+" 00:00:00");
object.setStartDate(startDate);

Date endDate=estFormat.parse(edate+" 23:59:59"); 
object.setEndDate(endDate);

假设 sdate 和 edate 是日期格式为 MM/dd/yyyy 的字符串。

解决方案:使用 JAVA-TIME API

    sdate=sdate.trim()+" 00:00:00";
    edate=edate.trim()+" 23:59:59";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
    LocalDateTime localdatetime = LocalDateTime.parse(sdate,df);
    Date startDate = Date.from(localdatetime.atZone(ZoneId.of("America/New_York" )).toInstant());
    object.setStartDate(startDate);

    localdatetime = LocalDateTime.parse(edate,df);
    Date endDate = Date.from(localdatetime.atZone(ZoneId.of("America/New_York" )).toInstant());
    object.setEndDate(endDate);

如果您想要 IST 计时,只需将第 2 行从 EST 更改为 IST:

estFormat.setTimeZone(TimeZone.getTimeZone("IST"));

tl;dr

LocalDate
.of( 2018 , Month.MAY , 23 )
.atStartOfDay(
    ZoneId.of( "America/New_York" )
)

Details

Never use the terrible old legacy date-time classes such as Date.

Use modern java.time classes.

ZoneId z = ZoneId.of( "America/New_York" ) ;
LocalDate ld = LocalDate.of( 2018 , Month.MAY , 23 ) ;
ZonedDateTime zdtStart = ld.atStartOfDay( z ) ;

When tracking an entire day, do not try to nail down the last moment. We commonly define a span of time using the Half-Open approach where the beginning is inclusive while the ending is exclusive. So a day starts with the first moment (typically at 00:00, but not always), and runs up to, but does not include, the first moment of the next day.

LocalDate dayAfter = ld.plusDays( 1 ) ;
ZonedDateTime zdtStop = dayAfter.atStartOfDay( z ) ;

Tip: Add the ThreeTen-Extra library to your project to access the Interval class.

org.threeten.extra.Interval interval = 
    Interval.of( 
        zdtStart.toInstant() ,
        zdtStop.toInstant()
    )
;

The class carries handy comparison methods such as abuts, contains, encloses, intersection, and more.

boolean containsMoment = interval.contains( Instant.now() ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.