package com.nanometer.smartlab.util; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang.time.DateUtils; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.math.BigInteger; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * Created by johnny on 15/9/9. */ @Component @Scope("singleton") public class Utils { public final static String[] DATE_FORMAT = new String[]{"yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd","yyyyMMddHHmmss","yyyy-MM-dd HH:mm:ss","yyyy/MM/dd HH:mm:ss"}; public static Timestamp now() { return new Timestamp(System.currentTimeMillis()); } public static String now(String format) { try { if (StringUtils.isBlank(format)) { format = Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Timestamp(System.currentTimeMillis())); } catch (Exception e) { e.printStackTrace(); } return null; } public static String formatDateToString(Date date, String pattern) { if (date != null) { return formatTimestampToString(new Timestamp(date.getTime()), pattern); } return null; } public static String formatTimestampToString(Timestamp time, String pattern) { try { if (time == null) { return null; } if (StringUtils.isBlank(pattern)) { pattern = Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(time); } catch (Exception e) { e.printStackTrace(); } return null; } public static Timestamp formatStringToTimestamp(String time, String pattern) { try { if (StringUtils.isBlank(time)) { return null; } if (StringUtils.isBlank(pattern)) { pattern = Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); return new Timestamp(sdf.parse(time).getTime()); } catch (Exception e) { e.printStackTrace(); } return null; } public static Timestamp formatStartTime(Timestamp time) { try { if (time == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return new Timestamp(calendar.getTimeInMillis()); } catch (Exception e) { e.printStackTrace(); } return null; } public static Timestamp formatEndTime(Timestamp time) { try { if (time == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return new Timestamp(calendar.getTimeInMillis()); } catch (Exception e) { e.printStackTrace(); } return null; } public static int getBetweenDay(Timestamp time1, Timestamp time2) { Calendar d1 = new GregorianCalendar(); d1.setTime(time1); Calendar d2 = new GregorianCalendar(); d2.setTime(time2); if (d1.after(d2)) { d1.setTime(time2); d2.setTime(time1); } int days = d2.get(Calendar.DAY_OF_YEAR)- d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { do { days += d1.getActualMaximum(Calendar.DAY_OF_YEAR); d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return days; } public static int getBetweenMonth(Timestamp time1, Timestamp time2) { Calendar d1 = new GregorianCalendar(); d1.setTime(time1); Calendar d2 = new GregorianCalendar(); d2.setTime(time2); if (d1.after(d2)) { d1.setTime(time2); d2.setTime(time1); } int months = d2.get(Calendar.MONTH) - d1.get(Calendar.MONTH); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { do { months += d1.getActualMaximum(Calendar.MONTH) + 1; d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return months; } public static Timestamp addTimeBySecond(Timestamp time, int second) { return addTime(time, Calendar.SECOND, second); } public static Timestamp subTimeBySecond(Timestamp time, int second) { return subTime(time, Calendar.SECOND, second); } public static Timestamp subTimeByMonth(Timestamp time, int month) { return subTime(time, Calendar.MONTH, month); } public static Timestamp addTimeByYear(Timestamp time, int year) { return addTime(time, Calendar.YEAR, year); } public static Timestamp subTimeByYear(Timestamp time, int year) { return subTime(time, Calendar.YEAR, year); } private static Timestamp addTime(Timestamp time, int type, int value) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time.getTime()); calendar.add(type, value); return new Timestamp(calendar.getTimeInMillis()); } private static Timestamp subTime(Timestamp time, int type, int value) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time.getTime()); calendar.add(type, -value); return new Timestamp(calendar.getTimeInMillis()); } public static String getWeekMonday(String format){ SimpleDateFormat formater=new SimpleDateFormat(format); Calendar cal=new GregorianCalendar(); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setTime(new Date()); cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); Date first=cal.getTime(); return formater.format(first); } }