2016-02-26 38 views
0

我想改变一个php脚本为java,但我卡在这两行代码。有谁知道等效代码是什么?从php到Java - mktime和日期

$Menarche = mktime(2, 0, 0, $Month, $Dday, $Year); 
$DueDate = $Menarche + 86400*(280 + ($MCL - 28)); 
+0

你有没有尝试过任何东西 – bmarkham

+0

使用java.util.Calendar –

+0

为什么不从[教程](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html)开始。 ['LocalDateTime.of'](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#of-int-int-int-int-int-int-)for第一行和['LocalDateTime.plus'](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#plus-long-java.time.temporal.TemporalUnit- )为第二部分 - 不需要乘以天数达到秒,只需使用正确的单位开始。只需使用['Duration'](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html)。 –

回答

0

你只需要创建一个从数据LocalDateTime。这是特定日期和时间的表示,但没有时区。

final int month = 11; 
final int day = 5; 
final int year = 1605; 

final LocalDateTime localDateTime = LocalDateTime.of(year, month, day, 2, 0, 0); 

我们打印出来,我们可以使用一个DateTimeFormatter

final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; 
System.out.printf("A date to remember: %s%n", localDateTime.format(formatter)); 

输出:

一个日期要记住:1605-11-05T02:00:00

LocalDateTime做算术运算我们可以简单地使用很多plusXXX层的方法,例如使用三个月:

final LocalDateTime dueDate = localDateTime.plusMonths(9); 
System.out.printf("Nine months later: %s%n", dueDate.format(formatter)); 

输出:

一个日期要记住:1606-08-05T02:00:00

或者,如果我们有一个精确持续时间 - 我们不能使用月份,因为它们是“估计” - 您可以创建一个Duration

final Duration gestation = Duration.ofDays(280); 
final LocalDateTime dueDate = localDateTime.plus(gestation); 
+0

非常感谢!这非常有帮助! – Vivian