时间日期汇总
1.计算2023年7月的最大天数
Calendar time=Calendar.getInstance();
time.clear();
time.set(Calendar.YEAR,2023);
time.set(Calendar.MONTH,7-1);//注意,Calendar对象默认一月为0,7-1是7月
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
System.out.println(day);
注意:在使用set方法之前,最好先clear一下,否则很多信息会继承自系统当前时间
2.Calendar和Date的转化
(1) Calendar转化为Date
Calendar calendar=Calendar.getInstance();
Date date=calendar.getTime();
(2) Date转化为Calendar
Date date=new Date();
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
3.格式化输出日期时间
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(df.format(date));
Date date=new Date();
System.out.printf("%tF %<tT",date);
System.out.printf("%tF %<tT %<tp %<tA %<tB %<tz%n",date);
//日期 时间 上下午 星期 月份 时区,换行
两种都可以
在Java中,可以使用以下转义序列来格式化日期和时间:
%tB
:完整的月份名称,例如:January%tb
:月份的缩写,例如:Jan%tm
:两位数的月份,例如:01表示一月,12表示十二月%tA
:完整的星期几名称,例如:Sunday%ta
:星期几的缩写,例如:Sun%td
:两位数的日期,例如:01表示1号,31表示31号%tY
:四位数的年份,例如:2023%ty
:两位数的年份,例如:23表示2023年%tH
:24小时制的小时数,例如:00表示午夜0点,23表示晚上11点%tI
:12小时制的小时数,例如:01表示凌晨1点,12表示中午12点%tM
:两位数的分钟数,例如:00表示整点,59表示59分%tS
:两位数的秒数,例如:00表示整分,59表示59秒%tL
:三位数的毫秒数,例如:000表示0毫秒,999表示999毫秒%tN
:九位数的纳秒数,例如:000000000表示0纳秒,999999999表示999999999纳秒%tp
:上午或下午的标记,例如:AM或PM%tz
:时区偏移量,例如:+0800表示东八区
4.计算一年中的第几星期
(1)计算某一天是一年中的第几星期
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DAY_OF_MONTH, 3);
int weekno=cal.get(Calendar.WEEK_OF_YEAR);
System.out.println("是一年中的第"+weekno+"星期");
(2)计算一年中的第几星期是几号
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2023);
cal.set(Calendar.WEEK_OF_YEAR, 1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("2023年第一个星期是"+df.format(cal.getTime()));
5.add()和roll()的用法
(1)add()方法
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.add(Calendar.DATE, -4);
Date date=cal.getTime();
System.out.println(df.format(date));
cal.add(Calendar.DATE, 4);
date=cal.getTime();
System.out.println(df.format(date));
//输出:
//2006-08-30
//2006-10-03
(2)roll方法
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.roll(Calendar.DATE, -4);
Date date=cal.getTime();
System.out.println(df.format(date));
cal.roll(Calendar.DATE, 4);
date=cal.getTime();
System.out.println(df.format(date));
//输出:
//2006-10-30
//2006-10-03
roll()方法在本月内循环,一般使用add()方法;
6.计算两个任意时间中间的间隔天数
Date d1 = new Date();
Date d2 = new Date(2003 - 1900, 5 - 1, 3, 16, 13, 30);
long d11 =d1.getTime();
long d22 =d2.getTime();
long d12 =(d11-d22)/(24*60*60*1000);
System.out.println("相隔"+d12+"天");
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2003,5-1,3);
long c11= calendar1.getTimeInMillis();
long c22 = calendar2.getTimeInMillis();
long c12 = (c11-c22)/(24*60*60*1000);
System.out.println(c12);
LocalDateTime dateTime1 = LocalDateTime.of(2003, 5, 3, 16, 13, 30); // 设置第一个日期时间
LocalDateTime dateTime2 = LocalDateTime.now(); // 设置第二个日期时间
long diffInDays = ChronoUnit.DAYS.between(dateTime1.toLocalDate(), dateTime2.toLocalDate());
System.out.println("相隔" + diffInDays+"天");
同理,可以用相同的方法计算出任意两个时间相隔的小时数,分钟数,秒钟数等
7. 获取月份中第一天和最后一天
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = "2022-05";
//本月第一天
Date startTime = sdf.parse(time+"-01");
Calendar calendar = Calendar.getInstance();
calendar.setTime(startTime);
calendar.add(Calendar.MONTH,1);//月增加1天
calendar.add(Calendar.DAY_OF_MONTH,-1);//日期倒数一日,既得到本月最后一天
//本月最后一天
Date endTime = calendar.getTime();
8. 获取两个日期中所有月份
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = "2022-05";
//本月第一天
Date startTime = sdf.parse(time+"-01");
Calendar calendar = Calendar.getInstance();
calendar.setTime(startTime);
calendar.add(Calendar.MONTH,1);//月增加1天
calendar.add(Calendar.DAY_OF_MONTH,-1);//日期倒数一日,既得到本月最后一天
//本月最后一天
Date endTime = calendar.getTime();
//获取系统当前时间:
public static String getSystemTime(){
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
//字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M/dd/yyyy hh:mm:ss a",java.util.Locale.US);
java.util.Date d = sdf.parse("5/13/2003 10:31:37 AM");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime1=formatter.format(d);
//当前时间
Calendar cal = Calendar.getInstance();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");
String mDateTime=formatter.format(cal.getTime());
//1年前日期
java.util.Date myDate=new java.util.Date();
long myTime=(myDate.getTime()/1000)-60*60*24*365;
myDate.setTime(myTime*1000);
String mDate=formatter.format(myDate);
//明天日期
myDate=new java.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
//两个时间之间的天数
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date= myFormatter.parse("2003-05-1");
java.util.Date mydate= myFormatter.parse("1899-12-30");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
//加半小时
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date date1 = format.parse("2002-02-28 23:16:00");
long Time=(date1.getTime()/1000)+60*30;
date1.setTime(Time*1000);
String mydate1=formatter.format(date1);
//年月周求日期
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
java.util.Date date2= formatter2.parse("2003-05 5 星期五");
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
//求是星期几
mydate= myFormatter.parse("2001-1-1");
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
String mydate3=formatter4.format(mydate);