标签:stat day mon except exce 利用 exception 输出 pack
1 package classwork8; 2 3 import java.util.Date; 4 5 public class DateTest { 6 7 public static void main(String[] args) { 8 Date currentDate = new Date(); 9 System.out.println("当前日期时间=" + currentDate); 10 String out = "自1970年1月1日起经历的毫秒数:"; 11 System.out.println(out + currentDate.getTime()); 12 Date newDate = new Date(100000000); 13 System.out.println("经过100000000毫秒后的新日期=" + newDate); 14 System.out.println("两日期的先后比较" + currentDate.before(newDate)); 15 System.out.println("两日期的先后比较" + currentDate.after(newDate)); 16 } 17 18 }
1 package classwork8; 2 3 import java.util.Calendar; 4 5 public class CalendarTest { 6 7 public static void main(String[] args) { 8 //获取当前日期对象 9 Calendar calendar=Calendar.getInstance(); 10 //利用calendar对象输出当前日期 11 System.out.println("当前时间:"+calendar.getTime()); 12 System.out.println("当前年份:"+calendar.get(Calendar.YEAR)); 13 System.out.println("当前月份:"+calendar.get(Calendar.MONTH));//返回值加1为当前月份 14 System.out.println("当前日期:"+calendar.get(Calendar.DATE)); 15 System.out.println("当前小时(12小时制):"+calendar.get(Calendar.HOUR)); 16 System.out.println("当前小时(24小时制):"+calendar.get(Calendar.HOUR_OF_DAY)); 17 System.out.println("当前星期"+calendar.get(Calendar.DAY_OF_WEEK));//返回值1~7代表周日到周六,返回值为4,代表周四 18 19 } 20 21 }
1 //日期时间格式化 2 package classwork8; 3 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 //Date date = new Date(); //获取当前系统时间,格式为: Thu Nov 01 18:27:06 CST 2018 8 //format: 按指定的目标格式把Date对象转换为String 9 //parse: 按指定的源格式把String转换为Date对象 10 public class SimpleDateFormateTest { 11 12 public static void main(String[] args) { 13 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); 14 SimpleDateFormat sdf2 = new SimpleDateFormat("yy/MM/dd HH:mm"); 15 SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 16 Date now = new Date(); 17 System.out.println("输出格式1:" + sdf1.format(now)); 18 System.out.println("输出格式2:" + sdf2.format(now)); 19 System.out.println("输出格式3:" + sdf3.format(now)); 20 String s = "2020-09-15 23:36"; 21 Date date; 22 try { 23 date = sdf3.parse(s); 24 System.out.println("输出格式4:" + date); 25 } catch (ParseException e) { 26 e.printStackTrace(); 27 } 28 } 29 30 }
标签:stat day mon except exce 利用 exception 输出 pack
原文地址:https://www.cnblogs.com/dss-99/p/14111710.html