我尝试将startDate的时间传递为00:00:00,将endDate的时间传递为23:59:59,但是在调试过程中,startDate的时间是清华8月09日10:30:00 IST 2018,endDate的时间是Tue Aug 14 10:29:59 IST 2018.Where我做错了吗?
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);发布于 2018-10-16 14:12:23
tl;dr
LocalDate
.of( 2018 , Month.MAY , 23 )
.atStartOfDay(
ZoneId.of( "America/New_York" )
)详细信息
永远不要使用可怕的旧的遗留日期-时间类,比如Date。
使用现代的java.time类。
ZoneId z = ZoneId.of( "America/New_York" ) ;
LocalDate ld = LocalDate.of( 2018 , Month.MAY , 23 ) ;
ZonedDateTime zdtStart = ld.atStartOfDay( z ) ;当跟踪一整天时,不要试图确定最后一刻。我们通常使用半开放方法来定义时间跨度,其中开始是包含的,而结束是排除的。因此,一天从第一个时刻开始(通常是在00:00,但不总是),直到第二天的第一个时刻,但不包括第二天的第一个时刻。
LocalDate dayAfter = ld.plusDays( 1 ) ;
ZonedDateTime zdtStop = dayAfter.atStartOfDay( z ) ;提示:将库添加到您的项目中以访问Interval类。
org.threeten.extra.Interval interval =
Interval.of(
zdtStart.toInstant() ,
zdtStop.toInstant()
)
;该类提供了方便的比较方法,如abuts、contains、encloses、intersection等。
boolean containsMoment = interval.contains( Instant.now() ) ;关于java.time
框架内置于Java8和更高版本中。这些类取代了麻烦的旧legacy日期时间类,如java.util.Date、Calendar和SimpleDateFormat。
现在在maintenance mode中的项目建议迁移到java.time类。
要了解更多信息,请参阅。和搜索堆栈溢出,以获得许多示例和解释。规范为JSR 310。
您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC driver。不需要字符串,也不需要java.sql.*类。
从哪里获取java.time类?
中的
项目使用额外的类扩展了java.time。这个项目是未来可能添加到java.time中的试验场。您可能会在这里找到一些有用的类,如Interval、YearWeek、YearQuarter和more。
发布于 2018-10-16 13:03:00
如果您需要IST时间,只需从EST更改为IST您的第2行:
estFormat.setTimeZone(TimeZone.getTimeZone("IST"));https://stackoverflow.com/questions/52828043
复制相似问题