Java 日期的相关操作

1.日期的转换

hh ==> 12小时制;

HH ==> 24小时制

Date time = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.format(date);

 

2.日期的加减

Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.YEAR,-1);//日期减1年
calendar.add(Calendar.MONTH, 1);//日期加1个月

3.转换为星期几

SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
String weekTime = dateFm.format(time);//输出 星期几

 4.获取当前月第一天和最后一天

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DAY_OF_MONTH, 1);
System.out.println (new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
cal.roll(Calendar.DAY_OF_MONTH, -1);
System.out.println (new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));

 

posted @ 2022-02-17 17:08  盐排骨  阅读(91)  评论(0)    收藏  举报