标签:rto ret 当前日期 根据 out 常用 lse redis 日期转换
涉及到日期计算和字符串与日期转换,转来转去很是麻烦,于是归纳总结一下。以下都可以用。
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
public void DateToString(){ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); System.out.println(dateString); }
public void StrToDate() throws ParseException { String dateStr = "2019-01-19"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; date = format.parse(dateStr); System.out.println(date); }
public void getMonthFirstDay() throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=Calendar.getInstance();//获取当前日期 cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 String monthFirstDay = format.format(cal.getTime()); System.out.println(monthFirstDay); }
public void getMonthLastDay() throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=Calendar.getInstance();//获取当前日期 cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); String monthLastDay = format.format(cal.getTime()); System.out.println(monthLastDay); }
public void getLastMonthFirstDay() throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=Calendar.getInstance();//获取当前日期 cal.add(Calendar.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 String monthFirstDay = format.format(cal.getTime()); System.out.println(monthFirstDay); }
public void testCalendar() throws ParseException { Calendar cal = Calendar.getInstance(); // 赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1 System.out.println("年:" + cal.get(Calendar.YEAR)); System.out.println("月:" + (cal.get(Calendar.MONTH) + 1)); System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + cal.get(Calendar.MINUTE)); System.out.println("秒:" + cal.get(Calendar.SECOND)); }
public void getBetweenDay() throws ParseException{ String dateStr1 = "2019-01-01"; String dateStr2 = "2019-01-19"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = format.parse(dateStr1); Date date2 = format.parse(dateStr2); Long inteverDay = (date2.getTime()-date1.getTime())/(1000*60*60*24); System.out.println(inteverDay); }
public void addCurrentDay() throws ParseException { String datess = "2019-02-28"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date parse = format.parse(datess); Calendar cal=Calendar.getInstance();//获取当前日期 cal.setTime(parse); cal.add(Calendar.DAY_OF_YEAR, +1); String monthFirstDay = format.format(cal.getTime()); System.out.println(monthFirstDay); }
public void CalToDate() throws ParseException { Calendar cal=Calendar.getInstance(); Date date=cal.getTime(); }
public void DateToCal() throws ParseException { Calendar cal=Calendar.getInstance(); Date date = new Date(); cal.setTime(date); }
public void getMonday(){ Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); //monday可以替换成任意星期几 c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); System.out.println(dateStr); }
public boolean isSameWeekDates(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR); if (0 == subYear) { if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)){ return true; } } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) { // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周 if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)){ return true; } } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) { if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)){ return true; } } return false; }
Redis入门教程(一) https://www.cnblogs.com/jichi/p/10285346.html
Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/10165538.html
Spring入门详细教程(二) https://www.cnblogs.com/jichi/p/10176601.html
Spring入门详细教程(三) https://www.cnblogs.com/jichi/p/10177004.html
Spring入门详细教程(四) https://www.cnblogs.com/jichi/p/10211475.html
标签:rto ret 当前日期 根据 out 常用 lse redis 日期转换
原文地址:https://www.cnblogs.com/jichi/p/10294077.html