一、Date转String
java.util.Date 直接调用Date的无参数构造方法 年:yyyy 月:MM 日:dd 时:HH 分:mm 秒:ss 毫秒:SSS 将日期按照指定的格式转换使用SimpleDateFormat ,它是java.text包下的,专门负责日期格式化 将Date转String:使用 .format()方法
import java. text. SimpleDateFormat;
import java. util. Date;
public class Test {
public static void main ( String[ ] args) {
Date now = new Date ( ) ;
System. out. println ( now) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss SSS" ) ;
String str = sdf. format ( now) ;
System. out. println ( str) ;
}
}
java.util.Date类的toString()方法已经被重写,输出的不是内存地址 ,见源码:
public String toString ( ) {
BaseCalendar. Date date = normalize ( ) ;
StringBuilder sb = new StringBuilder ( 28 ) ;
int index = date. getDayOfWeek ( ) ;
if ( index == BaseCalendar. SUNDAY) {
index = 8 ;
}
convertToAbbr ( sb, wtb[ index] ) . append ( ' ' ) ;
convertToAbbr ( sb, wtb[ date. getMonth ( ) - 1 + 2 + 7 ] ) . append ( ' ' ) ;
CalendarUtils. sprintf0d ( sb, date. getDayOfMonth ( ) , 2 ) . append ( ' ' ) ;
CalendarUtils. sprintf0d ( sb, date. getHours ( ) , 2 ) . append ( ':' ) ;
CalendarUtils. sprintf0d ( sb, date. getMinutes ( ) , 2 ) . append ( ':' ) ;
CalendarUtils. sprintf0d ( sb, date. getSeconds ( ) , 2 ) . append ( ' ' ) ;
TimeZone zi = date. getZone ( ) ;
if ( zi != null) {
sb. append ( zi. getDisplayName ( date. isDaylightTime ( ) , TimeZone. SHORT, Locale. US) ) ;
} else {
sb. append ( "GMT" ) ;
}
sb. append ( ' ' ) . append ( date. getYear ( ) ) ;
return sb. toString ( ) ;
}
System.out.println(now); 与 System.out.println(now.toString()); 结果一样,因为println重写了toString()方法 ,见源码:
public void println ( Object x) {
String s = String. valueOf ( x) ;
synchronized ( this ) {
print ( s) ;
newLine ( ) ;
}
}
public static String valueOf ( Object obj) {
return ( obj == null) ? "null" : obj. toString ( ) ;
}
二、String转Date
将String转Date:使用 .parse()方法,同时 throws ParseException
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
public class Test {
public static void main ( String[ ] args) throws ParseException {
String time = "1970-01-01 00:00:00" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date date = sdf. parse ( time) ;
System. out. println ( date) ;
}
}
ParseException异常 字符串的日期格式和SimpleDateFormat对象指定的日期格式两者不一致时,会抛出ParseException异常
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
public class Test04 {
public static void main ( String[ ] args) throws ParseException {
String time = "1970-01-01 00:00" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date date = sdf. parse ( time) ;
System. out. println ( date) ;
}
}
三、.currentTimeMillis()
1. 获取自1970年1月1日 00:00:00 000到当前系统时间的总毫秒数
public class Test {
public static void main ( String[ ] args) {
long nowTimeMillis = System. currentTimeMillis ( ) ;
System. out. println ( nowTimeMillis) ;
}
}
System.currentTimeMillis() 底层调用了C++ ,见源码:
public static native long currentTimeMillis ( ) ;
2. 统计一个方法的耗时
public class Test {
public static void main ( String[ ] args) {
long begin = System. currentTimeMillis ( ) ;
print ( ) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "耗时" + ( end - begin) + "毫秒" ) ;
}
public static void print ( ) {
for ( int i = 0 ; i < 1000000000 ; i++ ) {
}
}
}
四、通过给定的毫秒构造Date对象
有参构造方法:public Date(long date) date是一个long类型的毫秒数
import java. text. SimpleDateFormat;
import java. util. Date;
public class Test {
public static void main ( String[ ] args) {
Date time = new Date ( 1 ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss SSS" ) ;
String strTime = sdf. format ( time) ;
System. out. println ( strTime) ;
Date time2 = new Date ( System. currentTimeMillis ( ) - 1000 * 60 * 60 * 24 ) ;
String strTime2 = sdf. format ( time2) ;
System. out. println ( strTime2) ;
}
}