1.系统时间
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; public class DateDemo1 { /** * 时间表示法 */ public static void main(String[] args) { // long表示法 long now = System.currentTimeMillis();//获取系统时间毫秒值 System.out.println("计算机时间: " + now); //date表示法 Date date = new Date(); System.out.println("util.Date : " + date); //子类 java.sql.Date sDate = new java.sql.Date(now); System.out.println("sql.Date : " + sDate); Time time = new Time(now); System.out.println("time : " + time); Timestamp ts = new Timestamp(now); System.out.println("timestamp : " + ts); //calendar表示法 Calendar cal = Calendar.getInstance();//获取实例 System.out.println(cal.getClass().getName()); System.out.println(cal); } }
2.Date类
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; public class DateDemo2 { /** * date类 */ public static void main(String[] args) { Date date = new Date();//系统时间 System.out.println("util.Date : " + date); // Date d2 = new Date(2014,6,15);//已过时 // System.out.println("d2 : " + d2); Date d3 = new Date(System.currentTimeMillis() - 1000 * 60 * 60); System.out.println("d3 : " + d3); System.out.println("d3.before(date) : " + d3.before(date)); System.out.println("d3.after(date) : " + d3.after(date)); System.out.println("d3.getTime() : " + d3.getTime()); } }
3.calendar类
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; public class DateDemo3 { /** * calendar类 */ public static void main(String[] args) { Calendar cal = Calendar.getInstance(); //获取信息 int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1;// 0 - 11 1 -12 int day = cal.get(Calendar.DATE); System.out.println("year = " + year); System.out.println("month = " + month); System.out.println("day = " + day); //System.out.println(Calendar.DATE); 星期日 1 - 7 System.out.println("星期 : " + cal.get(Calendar.DAY_OF_WEEK)); System.out.println("今年第几天 : " + cal.get(Calendar.DAY_OF_YEAR)); System.out.println("星期四 : " + Calendar.THURSDAY); //设置 // cal.set(Calendar.YEAR, 2013); // cal.set(Calendar.MONTH, 2); // cal.set(Calendar.DATE, 15); // // cal.set(2015, 0, 31, 8, 30, 25); // // System.out.println("cal = " + String.format("%tF %<tT", cal)); // //计算 cal.add(Calendar.DATE, -100); System.out.println("cal = " + String.format("%tF", cal)); } }
4.日期格式化
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; public class DateDemo4 { /** * 格式化 */ public static void main(String[] args) { Date date = new Date(); System.out.println("date = " + String.format("%tF %tT", date,date)); Calendar cal = Calendar.getInstance(); System.out.println("cal = " + String.format("%tF %<tT", cal)); //字符串格式化 System.out.printf("%d ",123);//格式控制符 System.out.printf("%c ",97);//格式控制符 //格式化方法 String result = String.format("%c", 65); System.out.println("result = " + result ); } }
5.格式化对象
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class DateDemo5 { /** * 格式化 */ public static void main(String[] args) { Date date = new Date(); System.out.println("date = " + date); //格式化对象 //DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); //DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); System.out.println("实现类 : " + df.getClass().getName()); System.out.println(df.format(date)); df = DateFormat.getTimeInstance(DateFormat.LONG); System.out.println(df.format(date)); df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT); System.out.println(df.format(date)); } }
6.SimpleDateFormat
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateDemo6 { /** * 格式化 */ public static void main(String[] args) { Date date = new Date(); System.out.println("date = " + date); //格式化对象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy@MM@dd HH%mm%ss"); System.out.println(sdf.format(date)); sdf.applyPattern("dd日MM月yyyy年"); System.out.println(sdf.format(date)); } }
7.日期对象转化
package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateDemo7 { /** * 对象转化 */ public static void main(String[] args) { //long Date ud = new Date(); //util.Date -> sql.Date java.sql.Date sd = new java.sql.Date(ud.getTime()); //sql.Date -> util.Date ud.setTime(sd.getTime()); //util.Date -> Calendar Calendar cal = Calendar.getInstance(); cal.setTime(ud); //Calendar -> util.Date ud = cal.getTime(); String s = "2008-08-08"; //sd = s; sd = java.sql.Date.valueOf(s); System.out.println(sd); } }