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