三种日期工具
配合下面的案例可以更加深度的了解
Date
需要导包java.util.Date
Date d = new Date();
//两种都是获取到现在时间的时间戳
long t1 = d.getTime();//这里返回的是long类型的时间戳
long t2 = System.currentTimeMillis();//同样返回的是long类型的时间戳
Date d3 =new Date(t2);//将long类型转成Date
Calendar
需要导包java.util.Calendar
Calendar c = Calendar.getInstance();
c.set(2021, 5 - 1, 18);//这里返回的是Date
int year = c.get(1);
System.out.println(year);
int month = c.get(Calendar.MONTH) + 1;//0代表一月,以此类推应当加一
System.out.println(month);
int day = c.get(Calendar.DAY_OF_MONTH);//当月中的第几天
System.out.println(day);
int hh = c.get(Calendar.HOUR);//12小时
hh = c.get(Calendar.HOUR_OF_DAY);//24小时
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 wk = c.get(Calendar.DAY_OF_WEEK);
//1 星期日 2 星期一 3星期二 4 星期三 5 星期四 6星期五 7星期六
//System.out.println(wk);
SimpleDateFormat
需要导包java.text.SimpleDateFormat
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式
System.out.println(sdf.format(d));//初始化date
System.out.println(d.getTime()); //返回一个时间戳
Date birth = sdf.parse("2001-7-20 0:0:0");定义一个时间点
三种综合案例
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Date01 {
public static void main(String[] args) {
Date d = new Date();
//时间戳 long 数字
long t1 = d.getTime();
long t2 = System.currentTimeMillis();
//将long转成Date
Date d3 = new Date(t2);
Calendar c = Calendar.getInstance();
c.set(2021, 5 - 1, 18);
//将Calendar 日历类实例,转成Date
Date d2 = c.getTime();
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(s.format(d));
System.out.println(s.format(d2));
}
}
public class Date02 {
public static void main(String[] args) throws ParseException {
//使用Date 导入并实例化,java.util.Date
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d));
System.out.println(d.getTime()); //返回一个时间戳
long now = d.getTime() + (1000 * 60 * 60 * 24 * 10);
d.setTime(now);
System.out.println(sdf.format(d));
//计算两个日期之间相关的天数
Date birth = sdf.parse("2001-7-20 0:0:0");
//将字符串解析转换为Date
Date ddd = new Date();
long days = (ddd.getTime() - birth.getTime())/1000/60/60/24;
System.out.println(days);
}
}
public class Date03 {
public static void main(String[] args) throws ParseException {
Locale.setDefault(Locale.US);
Calendar c = Calendar.getInstance();
c.set(2021, 7 - 1, 27);
//year
int year = c.get(1);
System.out.println(year);
int month = c.get(Calendar.MONTH) + 1;
System.out.println(month);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
int hh = c.get(Calendar.HOUR);//12小时
hh = c.get(Calendar.HOUR_OF_DAY);//24小时
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 wk = c.get(Calendar.DAY_OF_WEEK);
//1 星期日 2 星期一 3星期二 4 星期三 5 星期四 6星期五 7星期六
//System.out.println(wk);
if (wk == 1) {
System.out.println("今日是星期日");
} else if (wk == 2) {
System.out.println("今日是星期一");
} else if (wk == 3) {
System.out.println("今日是星期二");
} else if (wk == 4) {
System.out.println("今日是星期三");
} else if (wk == 5) {
System.out.println("今日是星期四");
} else if (wk == 6) {
System.out.println("今日是星期五");
} else {
System.out.println("今日是星期六");
}
Locale.setDefault(Locale.CHINA);
System.out.printf("%tA%n",c);
Date d = new Date();
//0 星期日 1 星期一 6 星期六
int ww = d.getDay();
System.out.println(ww);
}
}