public class DateTimeTest { public static void main(String[] args) { // java.util.Date //获取系统当前date+time时间 // Date date = new Date(); // System.out.println(date); // 输出结果Wed Dec 08 09:26:54 CST 2021 //............................................................... //long 0:1970-1-1 0:0:0:0 //获取系统当前时间戳 // long time1 = new Date().getTime(); // long time2 = System.currentTimeMillis(); // System.out.println(time1); // 输出结果1638926883294 //构建指定时间戳的日期对象 // Date spec = new Date(12893829L); // System.out.println(spec); //2021-10-11 13:14:15 987 //java.text.SimpleDateFormat 日期格式化工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); //将日期对象格式化为指定字符串 // String format = sdf.format(new Date()); // System.out.println(format); // System.out.println(sdf.format(new Date())); //输出结果为2021-12-08 17:30:15 838 // 2021-12-08 09:29:45 595 //将指定日期格式字符串解析为日期对象 // try { // Date date = sdf.parse("2020-01-05 17:30:15 595");//输入参数格式要和sdf格式保持一致 // System.out.println(date); // } catch (ParseException e) { // e.printStackTrace(); // } //java.util.Calender //获取当前时区的日历对象 Calendar calendar = Calendar.getInstance(); //月份从0开始 // calendar.setTimeInMillis(17783930930L); // calendar.setTime(new Date()); calendar.set(2021,11,7,8,13,14);//月份从0开始,0~11; //获取当前日历表示的时间对象 Date time = calendar.getTime(); //获取当前日历表示的时间戳 long timeInMillis = calendar.getTimeInMillis(); //获取参数表示的当前日历的日期部分 世纪 年 季 月 月日 年日 周日 年周 月周 int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH)+1;//0~11 int date = calendar.get(Calendar.DATE); //24小时制 int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int millisecond = calendar.get(Calendar.MILLISECOND); //一年的第几天 int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); //某月中第几周。按这个月1号算,1号起就是第1周,8号起就是第2周。 int dayOfWeekInMonth = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); //该年的第几周(每年最后一周和新年第一周混合,则为第一周) int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR); //该年的第几季度 int quarter=(month-1)/3+1; //一周的第几天(周日为第一天) int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); //一个月的第几天 int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); //这个月的第几周(周日为下一周,第一周可以有1到7天,任意天数) int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH); //十二小时制 int hour = calendar.get(Calendar.HOUR); System.out.println("timeInMillis\t"+timeInMillis); System.out.println("year\t"+year); System.out.println("month\t"+month ); System.out.println("date\t"+date ); System.out.println("hourOfDay\t"+hourOfDay ); System.out.println("minute\t"+minute ); System.out.println("second\t"+second ); System.out.println(" millisecond\t"+ millisecond ); System.out.println("dayOfYear\t"+dayOfYear ); System.out.println("dayOfWeekInMonth\t"+ dayOfWeekInMonth); System.out.println("weekOfYear\t"+weekOfYear ); System.out.println("quarter\t"+quarter ); System.out.println("dayOfWeek\t"+ dayOfWeek); System.out.println("dayOfMonth\t"+dayOfMonth ); System.out.println("weekOfMonth\t"+weekOfMonth ); System.out.println("hour\t"+hour ); } }
运行结果:
timeInMillis 1638835994734
year 2021
month 12
date 7
hourOfDay 8
minute 13
second 14
millisecond 734
dayOfYear 341
dayOfWeekInMonth 1
weekOfYear 50
quarter 4
dayOfWeek 3
dayOfMonth 7
weekOfMonth 2
hour 8