//【java】String转Date与Date转String
1 //String转Date 2 Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-01-01 12:01:23"); 3 System.out.println(date);//Wed Jan 01 12:01:23 CST 2020 4 //Date转String 5 String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 6 System.out.println(time);//2020-01-03 14:18:29
//【java】判断今天是星期几
1 String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 2 Calendar cal = Calendar.getInstance(); 3 cal.setTime(new Date()); 4 int w = cal.get(Calendar.DAY_OF_WEEK) - 1; 5 if (w < 0) 6 w = 0; 7 String week = weekDays[w]; 8 System.out.println(week);//不差天数
//【java】判断两个时间的大小
1 String a = "11:11:11"; 2 String b = "12:11:11"; 3 String s1 = "2019-02-01"; 4 String s2 = "2019-02-02"; 5 System.out.println(a.compareTo(b));//结果是 -1 a<b 6 System.out.println(s2.compareTo(s1));//结果是 1 s2>s1
1 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 2 Date d1 = sdf.parse("2018-06-01"); 3 Date d2 = sdf.parse("2018-06-02"); 4 Date d3 = sdf.parse("2018-06-02"); 5 System.out.println(d1.before(d2));//true d1<d2 6 System.out.println(d2.before(d3));//false d2<d3 可以通过比较字符串是否相等来判断
//【java】两个时间相差的秒数
1 Date a = new Date(); 2 Date b = new Date(); 3 long second = (b.getTime() - a.getTime())/1000; 4 System.out.println(second);//结果为0
//【java】获取一个月有多少天(最大天数)
1 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 2 Calendar calendar = Calendar.getInstance(); 3 calendar.setTime(sdf.parse("2020-01-3")); 4 int num = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 5 System.out.println(num);//结果为31
//【java】单独获取时分秒
Date d = new Date(); int hours = d.getHours();//时 int minutes = d.getMinutes();//分 int second = d.getSeconds();//秒
//【java】获取当前日期n天后的日期
Calendar calendar2 = Calendar.getInstance(); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); calendar2.add(Calendar.DATE, 3); String daysDelay = sdf2.format(calendar2.getTime());
//【java】获取当前时间
1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 2 String nowtime = df.format(new Date());
//【js】获取当前时间 年月日
1 var date = new Date(); 2 var year = date.getFullYear(); 3 var month = date.getMonth() + 1; 4 var day = date.getDate(); 5 if (month < 10) { 6 month = "0" + month; 7 } 8 if (day < 10) { 9 day = "0" + day; 10 } 11 var nowDate = year + "-" + month + "-" + day;
持续更新!!