java.util.Date类
package com.wkcto.chapter04.datetime;
import java.util.Date;
/**
* 演示Date类
* @author 蛙课网
*
*/
public class Test01 {
public static void main(String[] args) {
//1) 无参构造 , 返回当前日期
Date date = new Date();
System.out.println( date );
//2) 通过构造方法指定一个从 1970-1-1 00:00:00 经过的毫秒数
long millis = System.currentTimeMillis(); //返回当前日期从 1970-1-1 00:00:00 经过的毫秒数
System.out.println( millis );
Date date2 = new Date(millis);
System.out.println( date2 );
date2 = new Date(millis + 1000*62);
System.out.println( date2 );
//
System.out.println( date.after(date2)); //false
System.out.println( date.getTime() ); //返回日期距离1970-1-1 00:00:00 经过的毫秒数
}
}
java.text.SimpleDateFormat 类
package com.wkcto.chapter04.datetime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Date与字符串之间的相互转换
* @author 蛙课网
*
*/
public class Test02 {
public static void main(String[] args) throws ParseException {
// 1) 创建Date对象 , 返回当前日期
Date date = new Date();
System.out.println(date); //Thu Aug 09 16:18:48 CST 2018
//2) 把Date转换为指定格式的字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS");
System.out.println( sdf.format(date) ); //2018年08月09日 16:21:46 295
//3)把日期字符串转换为Date对象
String text = "2088-08-08 10:11:12";
//注意: 格式串一定要完全匹配日期字符串
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = sdf.parse(text) ; //把文本按指定的格式转换为字符串
System.out.println( date );
}
}
java.util.Calendar类
package com.wkcto.chapter04.datetime;
import java.util.Calendar;
import java.util.Date;
/**
* 演示Calendar类
* @author 蛙课网
*
*/
public class Test03 {
public static void main(String[] args) {
//1) 获取当前日历
Calendar rightNow = Calendar.getInstance();
System.out.println( rightNow );
//java.util.GregorianCalendar[time=1533803370426,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=221,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=29,SECOND=30,MILLISECOND=426,ZONE_OFFSET=28800000,DST_OFFSET=0]
//2) 获得日历各个字段值
System.out.println( rightNow.get(Calendar.YEAR));
//注意 , Calendar.MONTH 字段的值是从0开始的
System.out.println( rightNow.get(Calendar.MONTH)); //7 , 代表8月
System.out.println( rightNow.get(Calendar.DAY_OF_MONTH));
System.out.println( rightNow.get(Calendar.HOUR_OF_DAY));
System.out.println( rightNow.get(Calendar.MINUTE));
System.out.println( rightNow.get(Calendar.SECOND));
//3)修改日历各个字段的值, 使用set()设置字段值时, 可能会影响到相邻字段的值
rightNow.set(Calendar.DAY_OF_MONTH, 40);
System.out.println( rightNow.get(Calendar.DAY_OF_MONTH)); //9
System.out.println( rightNow.get(Calendar.MONTH)); //8
//4)
Date date = rightNow.getTime(); //返回日历对应的Date日期
System.out.println( date );
long mills = rightNow.getTimeInMillis(); //返回日历对应的毫秒数
System.out.println( mills );
}
}
java.time包中的日期类
java.time包中的日期类是线程安全的
package com.wkcto.chapter04.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* LocalDateTime类
* @author 蛙课网
*
*/
public class Test04 {
public static void main(String[] args) {
LocalDate date = LocalDate.now() ;
System.out.println( date );
LocalTime time = LocalTime.now();
System.out.println( time);
LocalDateTime dateTime = LocalDateTime.now();
System.out.println( dateTime ); //2018-08-09T16:58:16.003
//对日期格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//LocalDateTime有一个实例方法format对日期格式化
System.out.println( dateTime.format(formatter));
//把字符串转换为日期
String text = "2088年08月08日 10:11:12";
//LocalDateTime有一个静态方法parse()可以把字符串转换为日期
dateTime = LocalDateTime.parse(text, formatter);
System.out.println( dateTime );
}
}