📄 dateutil.java
字号:
package com.tarena.util;import java.sql.Date;import java.sql.Timestamp;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;public class DateUtil { private static final long monthInterval = ((long) 24 * 60) * 60 * 1000 * 32; private static final String timestampStrFormat = "yyyy-MM-dd hh:mm:ss.SSS"; private static final String dateStrFormat = "yyyy-MM-dd hh:mm:ss"; public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } public static Date getCurrentDate() { return new Date(System.currentTimeMillis()); } public static boolean isMonthInterval(Timestamp time1, Timestamp time2) { long t1 = time1.getTime(); long t2 = time2.getTime(); if (Math.abs(t1 - t2) > monthInterval) return false; return true; } public static Timestamp getStartTimestampByYear(int year) { String dateStr = String.valueOf(year) + "-01-01 00:00:00.000"; SimpleDateFormat sdf = new SimpleDateFormat(timestampStrFormat); Timestamp t = null; try { t = new Timestamp(sdf.parse(dateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return t; } public static Timestamp getEndTimestampByYear(int year) { String dateStr = String.valueOf(year) + "-12-31 23:59:59.999"; SimpleDateFormat sdf = new SimpleDateFormat(timestampStrFormat); Timestamp t = null; try { t = new Timestamp(sdf.parse(dateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return t; } public static Timestamp getStartTimestampByYearAndMonth(int year, int month) { String dateStr = String.valueOf(year) + "-" + (month > 9 ? "" : "0") + String.valueOf(month) + "-01 00:00:00.000"; SimpleDateFormat sdf = new SimpleDateFormat(timestampStrFormat); Timestamp t = null; try { t = new Timestamp(sdf.parse(dateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return t; } public static Timestamp getEndTimestampByYearAndMonth(int year, int month) { String lastDay = String.valueOf(getLastDay(year, month)); String dateStr = String.valueOf(year) + "-" + (month > 9 ? "" : "0") + String.valueOf(month) + "-" + lastDay + " 23:59:59.999"; SimpleDateFormat sdf = new SimpleDateFormat(timestampStrFormat); Timestamp t = null; try { t = new Timestamp(sdf.parse(dateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return t; } public static Date getStartTimeByYearAndMonth(int year, int month) { String dateStr = String.valueOf(year) + "-" + (month > 9 ? "" : "0") + String.valueOf(month) + "-01 00:00:00"; SimpleDateFormat sdf = new SimpleDateFormat(dateStrFormat); Date date = null; try { date = new Date(sdf.parse(dateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return date; } public static Date getEndTimeByYearAndMonth(int year, int month) { String lastDay = String.valueOf(getLastDay(year, month)); String dateStr = String.valueOf(year) + "-" + (month > 9 ? "" : "0") + String.valueOf(month) + "-" + lastDay + " 23:59:59"; SimpleDateFormat sdf = new SimpleDateFormat(dateStrFormat); Date date = null; try { date = new Date(sdf.parse(dateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return date; } public static int getLastDay(int year, int month) { int[] monthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int value = 0; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { value = 1; } return monthDays[month - 1] + value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -