Java获取时间相关的代码
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance();
1.过去七天
c.setTime(new Date()); c.add(Calendar.DATE, - 7); Date d = c.getTime(); String day = format.format(d); System.out.println("过去七天:"+day);
2.过去一月
c.setTime(new Date()); c.add(Calendar.MONTH, -1); Date m = c.getTime(); String mon = format.format(m); System.out.println("过去一个月:"+mon);
3.过去三个月
c.setTime(new Date()); c.add(Calendar.MONTH, -3); Date m3 = c.getTime(); String mon3 = format.format(m3); System.out.println("过去三个月:"+mon3);
4.过去一年
c.setTime(new Date()); c.add(Calendar.YEAR, -1); Date y = c.getTime(); String year = format.format(y); System.out.println("过去一年:"+year);
5.现在时间24小时制
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowDate=format.format(new Date()); System.out.println(nowDate);
6.当天的开始时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); String start = format.format(c.getTime())+" 00:00:00"; System.out.println(start);
7.当天的截止时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); String end = format.format(calendar.getTime())+" 23:59:59"; System.out.println(end);
8.当前时间的前一个星期 开始时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, -6); String start = format.format(c.getTime())+" 00:00:00"; System.out.println(start);
9.当天日期前一个月
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); //得到前一个月 String start = format.format(c.getTime())+" 00:00:00"; System.out.println(start);
10.当前时间的前一年开始时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, -1); //年份减1 String start =format.format(c.getTime())+" 00:00:00"; System.out.println(start);
11.当前时间的周一时间和周末时间 setfirstdayofweek()方法
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd "); Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); String weekStart = format.format(c.getTime())+" 00:00:00"; System.out.println(weekStart); Calendar ca = Calendar.getInstance(); ca.setFirstDayOfWeek(Calendar.MONDAY); ca.set(Calendar.DAY_OF_WEEK, ca.getFirstDayOfWeek() + 6); // Sunday String weekEnd = format.format(ca.getTime())+" 23:59:59"; System.out.println(weekEnd);
12.当前时所在月份的第一天和最后一天
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 String monthStart = format.format(c.getTime())+" 00:00:00"; System.out.println(monthStart); Calendar ca = Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); String monthEnd = format.format(ca.getTime())+" 23:59:59"; System.out.println(monthEnd);
13.当期时间所在年份的开始时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.set(c.get(Calendar.YEAR) ,0, 1);//开始时间日期 String yearStart = format.format(c.getTime())+" 00:00:00"; System.out.println(yearStart); Calendar ca = Calendar.getInstance(); ca.set(ca.get(Calendar.YEAR) ,11, ca.getActualMaximum(Calendar.DAY_OF_MONTH));//结束日期 String yearEnd = format.format(ca.getTime())+" 23:59:59"; System.out.println(yearEnd);
14.计算时间差取得两个时间的微秒级的时间差
//得微秒级时间差 long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis(); //换算后得到天数 long day = val / (1000 * 60 * 60 * 24);
15.获取某个时间的上周一和周日
Calendar cal = Calendar.getInstance(); //n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推 int n = 1; String monday; cal.add(Calendar.DATE, n*7); //想周几,这里就传几Calendar.MONDAY(TUESDAY...) cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); System.out.println(monday);
越努力越幸运,一起加油吧!