--我有这个java类,每10秒在数据库中插入同一行。如何在每次插入中增加日期?
@Component
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
private final Logger log =
LoggerFactory.getLogger(ScheduledTasks.class);
@Autowired
StandardPlanServiceImpl standardplanDao;
@Autowired
ModalityServiceImpl modalityDao;
@Scheduled(fixedRate = 10000)
public void reportCurrentTime() throws Exception {
log.error("The time is now {}", dateFormat.format(new
Date()));
BigInteger bigIntegerNbpers=new BigInteger("15");
String dDate = "2019091804000000";
String dpDateTime = "2019091807000000";
String fDate = "2019091811000000";
String fpDateTime = "2019091807300000";
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date/time
.appendPattern("yyyyMMddHHmmss")
// milliseconds (with 2 digits)
.appendValue(ChronoField.MILLI_OF_SECOND, 2)
// create formatter
.toFormatter();
standardplanDao.save(new
StandardPlan("red",LocalDateTime.parse(dDate,
fmt),LocalDateTime.parse(dpDateTime,
fmt),LocalDateTime.parse(fDate,
fmt),LocalDateTime.parse(fpDateTime, fmt),bigIntegerNbpers,"groupe
Test","Bloc 2"));
log.error("The Saved Line : Début datetime: " +
LocalDateTime.parse(dDate, fmt)+ " Fin date time :" +
LocalDateTime.parse(fDate, fmt) + " Début pause date time : " +
LocalDateTime.parse(dpDateTime, fmt) + " Fin pause date time : " +
LocalDateTime.parse(fpDateTime, fmt));
List<StandardPlan> list = standardplanDao.getAllStandardPlan();
log.error("##########################
########Test##################################");
}
}这里是我的日志的结果.
:时间现在是2019-08-05 17:54:36+0200 :保存行:dé但日期时间: 2019-09-18T04:00 Fin date time :2019-09-18T11:00 Dé,但暂停日期: 2019-09-18T07:00 Fin暂停>日期: 2019-09-18T07:30
############################Test : The time is now 2019-08-05 17:54:46+0200 : The Saved Line : Début datetime: 2019-09-18T04:00 Fin date time :2019-09-18T11:00 Début pause date time : 2019-09-18T07:00 Fin pause > date time : 2019-09-18T07:30
############################Test
发布于 2019-08-05 16:59:01
您的时间被硬编码在以下几行
String dDate = "2019091804000000";
String dpDateTime = "2019091807000000";
String fDate = "2019091811000000";
String fpDateTime = "2019091807300000";您正在将它们解析为LocalDateTimes
LocalDateTime.parse(dpDateTime, fmt)解析之后,您可以使用plusXXX-函数(例如,plusDays(长日))来增加。https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
附带注意:如果dDate等是常量,则应该提取它们,以便它们不会在每次调用reportCurrentTime时被解析。
为了获得更好的可读性,您可以给它们更好的名称,比如dDate => debutDate
发布于 2019-08-05 16:58:24
您可以这样做,创建一个实例变量计数器,并在日期中添加10*counter秒。在函数的末尾增加计数器。
private int counter = 0;
...
LocalDateTime.parse(fpDateTime, fmt).plusSeconds(10*counter);
...
counter++;发布于 2019-08-05 17:11:15
由于在输出中使用分区时间,我建议使用ZonedDateTime类和DateTimeFormatter。下面是一个例子:
ZonedDateTime dt = ZonedDateTime.now();
System.out.println(dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ")));
System.out.println(dt.plus(10, ChronoUnit.SECONDS).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ")));https://stackoverflow.com/questions/57362654
复制相似问题