大家好,我是哪吒。
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//Sat Nov 06 18:31:32 CST 2021
}
System.out.println(date.getTime());//1636194765887
Calendar类的功能要比Date类强大很多,可以方便的进行日期的计算,获取日期中的信息时考虑了时区等问题。而且在实现方式上也比Date类要复杂一些。
Calendar类是一个抽象类,由于Calendar类是抽象类,且Calendar类的构造方法是protected的,所以无法使用Calendar类的构造方法来创建对象,API中提供了getInstance方法用来创建对象。
Calendar calendar = Calendar.getInstance();//默认是当前日期
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(2021, 5, 9);
System.out.println(calendar.getTime());//Wed Jun 09 18:36:09 CST 2021
}
(1)set设置
public final void set(int year,int month,int date)
(2)利用字段类型设置
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2021);
calendar.set(Calendar.MONTH,11);
calendar.set(Calendar.DAY_OF_MONTH,6);
System.out.println(calendar.getTime());//Mon Dec 06 18:39:22 CST 2021
}
Calendar类中用一下这些常量表示不同的意义,jdk内的很多类其实都是采用的这种思想
Calendar.YEAR——年份
Calendar.MONTH——月份
Calendar.DATE——日期
Calendar.DAY_OF_MONTH——日期,和上面的字段意义相同
Calendar.HOUR——12小时制的小时
Calendar.HOUR_OF_DAY——24小时制的小时
Calendar.MINUTE——分钟
Calendar.SECOND——秒
Calendar.DAY_OF_WEEK——星期几
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2021);
calendar.set(Calendar.MONTH,11);
calendar.set(Calendar.DAY_OF_MONTH,6);
calendar.add(Calendar.DAY_OF_MONTH,2);
System.out.println(calendar.getTime());//Wed Dec 08 18:42:25 CST 2021
}
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2021);
calendar.set(Calendar.MONTH,11);
calendar.set(Calendar.DAY_OF_MONTH,6);
calendar.add(Calendar.DAY_OF_MONTH,2);
System.out.println(calendar.get(Calendar.YEAR));//2021
System.out.println(calendar.get(Calendar.MONTH));//11
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//8
}
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();
}
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2021);
calendar.set(Calendar.MONTH,11);
calendar.set(Calendar.DAY_OF_MONTH,6);
calendar.add(Calendar.DAY_OF_MONTH,2);
long timeInMillis = calendar.getTimeInMillis();
}
(1)Date转Calendar
public static void main(String[] args) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
}
(2)Calendar转Date
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2021);
calendar.set(Calendar.MONTH,11);
calendar.set(Calendar.DAY_OF_MONTH,6);
calendar.add(Calendar.DAY_OF_MONTH,2);
Date time = calendar.getTime();
}
(1)Date转String
public static void main(String[] args) {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
}
(2)String转Date
public static void main(String[] args) throws ParseException {
String str="20211106";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(str);
System.out.println(date);
}
String转Date时,格式必须一致。
(1)Calendar 转化 String
public static void main(String[] args) throws ParseException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = sdf.format(calendar.getTime());
}
(2)String 转化Calendar
public static void main(String[] args) throws ParseException {
String str="2021-11-6";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println(calendar.getTime());//Sat Nov 06 00:00:00 CST 2021
}
本文收录于,49天精通Java从入门到就业。
全网最细Java零基础手把手入门教程,系列课程包括:基础篇、集合篇、Java8新特性、多线程、代码实战,持续更新中(每周1-2篇),适合零基础和进阶提升的同学。
哪吒多年工作总结:Java学习路线总结,搬砖工逆袭Java架构师。