获取今天的年月日
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = new Date();
String today = sdf.format(new Date());
System.out.println(“今天:”+today); //获取今天的年月日,2019-09-11
年月日 时分秒的格式:yyyy-MM-dd HH:mm:ss
指定日期字符串–转换为–日期格式Calendar
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date thisDate = sdf.parse(“2020-01-01”);
Calendar cld = Calendar.getInstance();
cld.setTime(thisDate);
String thisDay = sdf.format(cld.getTime());
System.out.println(“指定日期:”+thisDay); //获取指定日期的年月日,2020-01-01
获取指定日期所属月份的最后一天
第一种方法
//指定日期字符串–转换为–日期格式Calendar
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date thisDate = sdf.parse(“2019-09-15”);
Calendar cld = Calendar.getInstance();
cld.setTime(thisDate);//月份+1,天设置为0。下个月第0天,就是这个月最后一天
cld.add(Calendar.MONTH, 1);
cld.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf.format(cld.getTime());
System.out.println(“本月最后一天:”+lastDay); //2019-09-30cld.set(Calendar.DAY_OF_MONTH,1);
System.out.println(“本月第一天:”+ sdf.format(cld.getTime()) ); //2019-09-01
第二种方法,
据说Calendar类的set()有陷阱,保险起见还是用第一种方法吧,https://blog.csdn.net/freelk/article/details/78427207
//指定日期字符串–转换为–日期格式Calendar
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date thisDate = sdf.parse(“2019-09-15”);
Calendar cld = Calendar.getInstance();
cld.setTime(thisDate);
//月份+1,天设置为0。下个月第0天,就是这个月最后一天
cld.set(cld.get(Calendar.YEAR), cld.get(Calendar.MONTH)+1, 0);
String lastDay = sdf.format(cld.getTime());
System.out.println(“本月最后一天:”+lastDay); //2019-09-30
获取指定日期的上周的第一天、最后一天,即周一和周日
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);//设置日期格式
Calendar cld = Calendar.getInstance(Locale.CHINA); //使用中国时区得到一个日历
cld.setTime(sdf.parse(“2019-12-22”));
cld.add(Calendar.DATE, -7);
cld.setFirstDayOfWeek(Calendar.MONDAY); //以周一为首日cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //周一
System.out.println(sdf.format(cld.getTime()));cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //周日
System.out.println(sdf.format(cld.getTime()));
Calendar 的get方法
Calendar cld = Calendar.getInstance();
System.out.println(“Current Date: ” + cld.getTime()); //Current Date: Wed Sep 11 08:17:25 CST 2019
System.out.println(“Year: ” + cld.get(Calendar.YEAR)); //2019
System.out.println(“Month: ” + (cld.get(Calendar.MONTH) + 1)); //9,格林威治日历是0~11,所以要加1
System.out.println(“Day: ” + cld.get(Calendar.DATE)); //11,当前日期2019-09-11
System.out.println(“Hour: ” + cld.get(Calendar.HOUR)); //8,当前时间08:20:25
System.out.println(“HOUR_OF_DAY: ” + cld.get(Calendar.HOUR_OF_DAY)); //8,当前时间08:20:25
System.out.println(“Minute: ” + cld.get(Calendar.MINUTE)); //20,当前时间08:20:25
System.out.println(“Second: ” + cld.get(Calendar.SECOND)); //25,当前时间08:20:25
System.out.println(“DAY_OF_WEEK: ” + cld.get(Calendar.DAY_OF_WEEK)); //4,周三,西方周日是1
System.out.println(“DAY_OF_MONTH: ” + cld.get(Calendar.DAY_OF_MONTH)); //11,当前日期2019-09-11
System.out.println(“DAY_OF_YEAR: ” + cld.get(Calendar.DAY_OF_YEAR)); //254,当前日期2019-09-11System.out.println(“当前日期字符串:” + cld.get(Calendar.YEAR) + “-” + (cld.get(Calendar.MONTH) + 1) + “-” + cld.get(Calendar.DATE) + ” ”
+ cld.get(Calendar.HOUR) + “:” + cld.get(Calendar.MINUTE) + “:” + cld.get(Calendar.SECOND)); //2019-9-11 8:20:25
getActualMaximum方法:
之前有个BUG,如果设置2017年2月,获取的日期是2017-03-03,而其他月份没有问题,
原因是Calendar类在set的时候,并不会立即生效,只有在get的时候才会生效,
解决方法是在实例化Calendar类之后,要先调用clear()方法清空缓存。
Calendar calendar=Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.MONTH, 1);
int lastDay=calendar.getActualMaximum(calendar.DAY_OF_MONTH); //另外有getActualMinimum()
System.out.println("2017年2月最后一天是:"+lastDay); //28
calendar.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(calendar.getTime())); //2017-02-28