0
我想改變一個php腳本爲java,但我卡在這兩行代碼。有誰知道等效代碼是什麼?從php到Java - mktime和日期
$Menarche = mktime(2, 0, 0, $Month, $Dday, $Year);
$DueDate = $Menarche + 86400*(280 + ($MCL - 28));
我想改變一個php腳本爲java,但我卡在這兩行代碼。有誰知道等效代碼是什麼?從php到Java - mktime和日期
$Menarche = mktime(2, 0, 0, $Month, $Dday, $Year);
$DueDate = $Menarche + 86400*(280 + ($MCL - 28));
你只需要創建一個從數據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);
非常感謝!這非常有幫助! – Vivian
你有沒有嘗試過任何東西 – bmarkham
使用java.util.Calendar –
爲什麼不從[教程](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)。 –