- 日期和时间
- System.currentTimeMillis
- Date
- Java.util.Date
- Java.sql.Date
- 相互转换
- SimpleDateFormat
- Calendar
- LocalDate、LocalTime、LocalDateTime
package com.time;
public class TimeTest {
public static void main(String[] args) {
//时间戳
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
long currentTimeMillis = System.currentTimeMillis();
System.out.println(currentTimeMillis);//1635668404513
}
}
Date
Java.util.Date
package com.time;
import java.util.Date;
public class TimeTest {
public static void main(String[] args) {
dateTest();
}
public static void dateTest(){
//Java.util.Date创建当前时间
Date date = new Date();
//显示年月日
System.out.println(date);//Sun Oct 31 16:29:28 CST 2021
//获取时间戳
System.out.println(date.getTime());//1635669012760
//创建指定的时间戳对象
Date date1 = new Date(1635669012760L);
System.out.println(date1);//Sun Oct 31 16:30:12 CST 2021
}
}
Java.sql.Date
对应数据库中的日期类型
public static void test(){
java.sql.Date date = new java.sql.Date(1635669012760L);
System.out.println(date);//2021-10-31
}
相互转换
public static void utilToSql(){
Date date = new java.util.Date(1635669012760L);
//java.util.Date转为java.sql.Date对象步骤
//1、转为毫秒数
long time = date.getTime();
//2、用时间戳创建java.sql.Date的对象
java.sql.Date date1 = new java.sql.Date(time);
System.out.println(date1);//2021-10-31
}
SimpleDateFormat
对日期Date类的格式化与解析。
@Test
public void test2(){
Date date = new Date();
System.out.println(date);//Sun Oct 31 17:03:52 CST 2021
//实例化
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
//格式化
String format = simpleDateFormat.format(date);
System.out.println(format);//21-10-31 下午5:04
//解析
String str = "21-10-31 下午5:04";
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat();
try {
//要求字符串必须是符合SimpleDateFormat 识别的格式
Date parse = simpleDateFormat1.parse(str);//Sun Oct 31 17:04:00 CST 2021
System.out.println(parse);
} catch (ParseException e) {
e.printStackTrace();
}
//常用格式
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1 = simpleDateFormat2.format(date);
System.out.println(format1);//2021-10-31 05:11:06
}
Calendar
java.util.Calendar,日历类
@Test
public void calendarTest(){
//实例化
//方式一
Calendar calendar = new GregorianCalendar();
//方式二
Calendar instance = Calendar.getInstance();
//常用方法
//get()
int days = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(days);//这个月的第31天
//set()
instance.set(Calendar.DAY_OF_MONTH,21);//设置为这个月的第21天
int days2 = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(days2);//21
//add()
instance.add(Calendar.DAY_OF_MONTH,3);//在原有的基础上+3天
int days3 = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(days3);//24
//getTime(),转换为Date对象
Date time = calendar.getTime();
System.out.println(time);//Sun Oct 31 17:38:36 CST 2021
//setTime():Date转为日历类
Date date = new Date();
instance.setTime(date);
int days4 = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(days4);//31
}
LocalDate、LocalTime、LocalDateTime
@Test
public void test3(){
//实例化方式一
//now:获取当前日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2021-10-31
System.out.println(localTime);//17:53:56.515
System.out.println(localDateTime);//2021-10-31T17:53:56.515
//实例化方式二
//of():设置指定的年月日时分秒
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 10, 31, 15, 56);
System.out.println(localDateTime1);//2021-10-31T15:56
//getXxx():获取各种属性信息
System.out.println(localDateTime.getDayOfMonth());//31
System.out.println(localDateTime.getHour());//17
//withXxx():修改属性值,生成新的对象,原对象不会被改变
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
System.out.println(localDateTime);//2021-10-31T18:00:54.167
System.out.println(localDateTime2);//2021-10-22T18:00:54.167
//plusXxx():在原基础上相加,得到新对象,原对象不变
LocalDateTime localDateTime3 = localDateTime.plusDays(2);
System.out.println(localDateTime);//2021-10-31T18:04:44.357
System.out.println(localDateTime3);//2021-11-02T18:04:44.357
//minusXxx():在原基础上相减,得到新对象,原对象不变
LocalDateTime localDateTime4 = localDateTime.minusDays(2);
System.out.println(localDateTime);//2021-10-31T18:04:44.357
System.out.println(localDateTime4);//2021-10-29T18:06:53.532
}



