public class TestSimpleDateFormat {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();
//时间格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");
String format1 = format.format(date);
System.out.println(format1);
//格式化字符串日期
String date1 = "2024-04-07 20:29:11";
try {
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = format2.parse(date1);
System.out.println(parse);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
public class TestDate {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
long time = date.getTime();
//时间毫秒值
System.out.println(time);
time += 2*1000;
Date date1 = new Date(time);
System.out.println(date1);
Date date2 = new Date();
date2.setTime(time);
System.out.println(date2);
}
}
public class TestSimpleDateFormat {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();
//时间格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");
String format1 = format.format(date);
System.out.println(format1);
//格式化字符串日期
String date1 = "2024-04-07 20:29:11";
try {
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = format2.parse(date1);
System.out.println(parse);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
public class TestCalendar {
public static void main(String[] args) {
//系统此刻时间对应的日历对象
Calendar now = Calendar.getInstance();
System.out.println(now);
//取日历中的某个信息
System.out.println(now.get(Calendar.YEAR));
//时间毫秒
System.out.println(now.getTimeInMillis());
//修改日历中某个值
now.set(Calendar.MONTH, 9);
System.out.println(now);
//为某个信息添加或减少多少
now.add(Calendar.MONTH, 2);
now.add(Calendar.MONTH, -3);
System.out.println(now);
}
}