提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在Java 8中使用日期时间API获取日期和时间
import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.Month;// from N o w J a v a . c o m public class Main { public static void main(String[] args) { LocalDateTime ldt = LocalDateTime.now(); System.out.println("Local Date and Time: " + ldt); // Obtain the LocalDateTime object of the date 11/11/2000 at 12:00 LocalDateTime ldt2 = LocalDateTime.of(2000, Month.NOVEMBER, 11, 12, 00); System.out.println("Specified Date and Time: " + ldt2); // Obtain the month from LocalDateTime object Month month = ldt.getMonth(); int monthValue = ldt.getMonthValue(); /* N o w J a v a . c o m - 时 代 Java 提供 */ System.out.println("Month: " + month); System.out.println("Month Value: " + monthValue); // Obtain day of Month, Week, and Year int day = ldt.getDayOfMonth(); DayOfWeek dayWeek = ldt.getDayOfWeek(); int dayOfYr = ldt.getDayOfYear(); System.out.println("Day: " + day); System.out.println("Day Of Week: " + dayWeek); System.out.println("Day of Year: " + dayOfYr); // Obtain year int year = ldt.getYear(); System.out.println("Date: " + monthValue + "/" + day + "/" + year); int hour = ldt.getHour(); int minute = ldt.getMinute(); int second = ldt.getSecond(); System.out.println("Current Time: " + hour + ":" + minute + ":" + second); // Calculation of Months, etc. LocalDateTime currMinusMonths = ldt.minusMonths(12); LocalDateTime currMinusHours