1. import java.util.Calendar;
    2. public class CalenderTs {
    3. public static void main(String[] args) {
    4. Calendar c = Calendar.getInstance();
    5. int y = c.get(Calendar.YEAR);
    6. System.out.println(y);
    7. int m = c.get(Calendar.MONTH) + 1;
    8. System.out.println(m);
    9. int d = c.get(Calendar.DAY_OF_MONTH);
    10. System.out.println(d);
    11. int w = c.get(Calendar.DAY_OF_WEEK);
    12. System.out.println(w);
    13. int hh = c.get(Calendar.HOUR_OF_DAY);
    14. System.out.println(hh);
    15. int mm = c.get(Calendar.MINUTE);
    16. System.out.println(mm);
    17. int ss = c.get(Calendar.SECOND);
    18. System.out.println(ss);
    19. int ms = c.get(Calendar.MILLISECOND);
    20. System.out.println(y + "-" + m + "-" + d + " " + w + " " + hh + ":" + mm + ":" + ss + "." + ms);
    21. }
    22. }
    1. import java.text.SimpleDateFormat;
    2. import java.util.Calendar;
    3. import java.util.Date;
    4. public class CalenderDemo {
    5. public static void main(String[] args) {
    6. // 当前时间
    7. Calendar c = Calendar.getInstance();
    8. System.out.println(c);
    9. // 清除所有
    10. c.clear();
    11. // 设置年月日时分秒
    12. c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
    13. // 加5天并减去两小时
    14. c.add(Calendar.DAY_OF_MONTH, 5);
    15. c.add(Calendar.HOUR_OF_DAY, -2);
    16. // 显示时间
    17. var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    18. Date d = c.getTime();
    19. System.out.println(sdf.format(d));
    20. }
    21. }
    1. import java.time.LocalDate;
    2. import java.time.LocalDateTime;
    3. import java.time.LocalTime;
    4. public class LocalDateTimeDemo {
    5. public static void main(String[] args) {
    6. //当前日期
    7. LocalDate d = LocalDate.now();
    8. //当前时间
    9. LocalTime t = LocalTime.now();
    10. //当前日期和时间
    11. LocalDateTime dt = LocalDateTime.now();
    12. System.out.println(d);
    13. System.out.println(t);
    14. System.out.println(dt);
    15. }
    16. }
    1. import java.time.LocalDateTime;
    2. import java.time.format.DateTimeFormatter;
    3. public class DateTimeFormatterDemo {
    4. public static void main(String[] args) {
    5. //自定义格式化时间
    6. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
    7. System.out.println(dtf.format(LocalDateTime.now()));
    8. //用自定义格式解析
    9. LocalDateTime dt2 = LocalDateTime.parse("2019/11/30 15:16:17", dtf);
    10. System.out.println(dt2);
    11. }
    12. }
    1. import java.time.LocalDateTime;
    2. public class LocalDateTimeAddSub {
    3. public static void main(String[] args) {
    4. LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
    5. System.out.println(dt);
    6. //加5天减三个小时
    7. LocalDateTime dt2 = dt.plusDays(5).minusHours(3);
    8. //减一个月
    9. LocalDateTime dt3 = dt2.minusMinutes(1);
    10. System.out.println(dt3);
    11. }
    12. }
    1. import java.time.Instant;
    2. public class InstanceDemo {
    3. // 获取时间戳
    4. public static void main(String[] args) {
    5. Instant now = Instant.now();
    6. //秒
    7. System.out.println(now.getEpochSecond());
    8. //毫秒
    9. System.out.println(now.toEpochMilli());
    10. }
    11. }