2017-12-27 13 views

回答

0

只需在每个小时运行的cronjob,并产生只在时区刚刚结束

1

使用ZonedDateTime当天的报告:

String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; 
    String dateInString = "22-1-2015 10:15:55 AM"; 
    LocalDateTime ldt = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE_FORMAT)); 

    ZoneId singaporeZoneId = ZoneId.of("Asia/Singapore"); 
    System.out.println("TimeZone : " + singaporeZoneId); 

    //LocalDateTime + ZoneId = ZonedDateTime 
    ZonedDateTime asiaZonedDateTime = ldt.atZone(singaporeZoneId); 
    System.out.println("Date (Singapore) : " + asiaZonedDateTime); 

    ZoneId newYokZoneId = ZoneId.of("America/New_York"); 
    System.out.println("TimeZone : " + newYokZoneId); 

    ZonedDateTime nyDateTime = asiaZonedDateTime.withZoneSameInstant(newYokZoneId); 
    System.out.println("Date (New York) : " + nyDateTime); 

    DateTimeFormatter format = DateTimeFormatter.ofPattern(DATE_FORMAT); 
    System.out.println("\n---DateTimeFormatter---"); 
    System.out.println("Date (Singapore) : " + format.format(asiaZonedDateTime)); 
    System.out.println("Date (New York) : " + format.format(nyDateTime)); 

输出是:

TimeZone : Asia/Singapore 
Date (Singapore) : 2015-01-22T10:15:55+08:00[Asia/Singapore] 
TimeZone : America/New_York 
Date (New York) : 2015-01-21T21:15:55-05:00[America/New_York] 

---DateTimeFormatter--- 
Date (Singapore) : 22-1-2015 10:15:55 AM 
Date (New York) : 21-1-2015 09:15:55 PM 

使用方法从这里得到你需要的东西

取自:Java – Convert date and time between timezone

+0

,你已经抽样了一个日期,但我必须以午夜为参考。 – Rachit

+0

其中时区的午夜?如果案件与目标时区一致 - 您已全部解决 - 将dateInString设置为22-1-2015 00:00:00 AM,并使用“singaporeTimeZoneID”作为目标TZ,然后使用您的NewYork而不是NewYork。 如果相反,您想要检查每个时区的午夜时间,请将singaporeZoneId设置为您的时区并将newYorkZoneId设置为目标 – GuyKhmel

0

java.time

使用兼容JDBC 4.2或更高版本的驱动程序,Java的8或更高版本上运行,从使用该取代麻烦的旧的遗留日期时间类的现代java.time类受益。

确定“今天”和“昨天”意味着确定日期。确定一个日期需要一个时区。对于任何特定的时刻,日期因地区而异。

ZoneId z = ZoneId.of("America/Montreal") ; 
LocalDate today = LocalDate.now(z) ; 
LocalDate yesterday = today.minusDays(1) ; 

要查询数据库中的时间戳,我们需要特定的时刻。该问题指定午夜。术语“午夜”是模糊的。让我们用“一天中的第一时刻”来代替。

永不假设一天从00:00:00开始。夏令时(DST)等异常意味着它可能从另一个时间点开始,如01:00:00。让java.time确定一天中的第一个时刻。

ZonedDateTime start = yesterday.atStartOfDay(z) ; 

一般来说,为了确定时间跨度最好的办法是,虽然半开的方法,即最开始是包容而结局是独家。所以我们希望在一天的第一时间开始,并跑到下一个一天的第一时刻,但不包括。

ZonedDateTime stop = today.atStartOfDay(z) ; 

半打开,我们做使用SQL命令BETWEEN

SQL

SELECT * FROM t 
WHERE event >= ? AND event < ? ; 

的Java

myPreparedStatement.setObject(1 , start) ; 
myPreparedStatement.setObject(2 , stop) ; 

要检索的时间戳,使用getObject

Instant instant = myResultSet.getObject("event" , Instant.class) ; 

要从UTC移到区域,请应用ZoneId

ZonedDateTime zdt = instant.atZone(z) ;