zoukankan      html  css  js  c++  java
  • 时间工具类

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //

    package com.sprucetec.pdms.utils.util;

    import com.sprucetec.pdms.utils.exception.InvalidParamException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;

    public class DateUtil {
    public static final String YMD = "yyyy-MM-dd";
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String KEY_INT_STARTTIME = "intStarttime";
    public static final String KEY_STR_STARTTIME = "strStarttime";
    public static final String KEY_INT_ENDTIME = "intEndtime";
    public static final String KEY_STR_ENDTIME = "strEndtime";

    public DateUtil() {
    }

    public static int getCurrentTimeIntValue() {
    int sencends = (int)(System.currentTimeMillis() / 1000L);
    return sencends;
    }

    public static int getAppointedTimeIntValue(String time, String format) {
    SimpleDateFormat f = new SimpleDateFormat(format);
    Date date = null;

    try {
    date = f.parse(time);
    } catch (ParseException var5) {
    return -1;
    }

    return (int)(date.getTime() / 1000L);
    }

    public static Date getDateByTimeStamp(int timeStamp, String format) {
    SimpleDateFormat f = new SimpleDateFormat(format);
    String d = f.format(Long.valueOf((long)timeStamp * 1000L));
    Date date = null;

    try {
    date = f.parse(d);
    } catch (ParseException var6) {
    date = null;
    }

    return date;
    }

    public static String getDateStringByTimeStamp(Integer timeStamp, String format) {
    SimpleDateFormat f = new SimpleDateFormat(format);
    String d = "";
    if(timeStamp != null) {
    d = f.format(Long.valueOf((long)timeStamp.intValue() * 1000L));
    }

    return d;
    }

    public static String formatDate(Object datetime, String format) {
    SimpleDateFormat f = new SimpleDateFormat(format);
    if(datetime instanceof Date) {
    return f.format((Date)datetime);
    } else if(datetime instanceof Long) {
    return f.format(new Date(((Long)datetime).longValue()));
    } else {
    throw new InvalidParamException("param datetime must be a Date or Long.");
    }
    }

    public static int getTimesmorning(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(11, 0);
    cal.set(13, 0);
    cal.set(12, 0);
    cal.set(14, 0);
    return (int)(cal.getTimeInMillis() / 1000L);
    }

    public static int getTimesnight(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(11, 23);
    cal.set(13, 59);
    cal.set(12, 59);
    cal.set(14, 999);
    return (int)(cal.getTimeInMillis() / 1000L);
    }

    public static String formatDateToString(Date date, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return date == null?"":sdf.format(date);
    }

    public static Map<String, Object> formatStarttimeAndEndtime(String format, String strStarttime, String strEndtime, Integer intStarttime, Integer intEndtime) {
    HashMap timeMap = new HashMap();
    if(format == null) {
    timeMap.put("strStarttime", (Object)null);
    timeMap.put("intStarttime", (Object)null);
    timeMap.put("strEndtime", (Object)null);
    timeMap.put("intEndtime", (Object)null);
    return timeMap;
    } else {
    boolean ymd = false;
    boolean ymdhms = false;
    format = format.trim();
    int formatLen = format.length();
    int ymdLen = "yyyy-MM-dd".length();
    int ymdhmsLen = "yyyy-MM-dd HH:mm:ss".length();
    if(formatLen == ymdLen) {
    ymd = true;
    } else if(formatLen == ymdhmsLen) {
    ymdhms = true;
    }

    if(strStarttime != null && "".equals(strStarttime.trim())) {
    strStarttime = null;
    }

    if(strEndtime != null && "".equals(strEndtime.trim())) {
    strEndtime = null;
    }

    if(intStarttime != null && intStarttime.intValue() <= 0) {
    intStarttime = null;
    }

    if(intEndtime != null && intEndtime.intValue() <= 0) {
    intEndtime = null;
    }

    Date currentTime = new Date();
    int intCurrentTime = (int)(currentTime.getTime() / 1000L);
    int intTodayTimesmorning = getTimesmorning(currentTime);
    String strTodayTimesmorning = getDateStringByTimeStamp(Integer.valueOf(intTodayTimesmorning), format);
    int intTodayTimesnight = getTimesnight(currentTime);
    String strTodayTimesnight = getDateStringByTimeStamp(Integer.valueOf(intTodayTimesnight), format);
    if(strStarttime == null && strEndtime == null) {
    if((intStarttime != null || intEndtime != null) && (strStarttime == null || strEndtime == null) && strStarttime != null) {
    ;
    }
    } else {
    if(strStarttime != null && strEndtime != null) {
    intStarttime = Integer.valueOf(getAppointedTimeIntValue(strStarttime, format));
    intEndtime = Integer.valueOf(getAppointedTimeIntValue(strEndtime, format));
    } else if(strStarttime != null) {
    intStarttime = Integer.valueOf(getAppointedTimeIntValue(strStarttime, format));
    intEndtime = Integer.valueOf(intTodayTimesnight);
    strEndtime = strTodayTimesnight;
    } else {
    intEndtime = Integer.valueOf(getAppointedTimeIntValue(strEndtime, format));
    if(ymdhms) {
    strStarttime = "1970-01-01 00:00:00";
    } else {
    strStarttime = "1970-01-01";
    }

    intStarttime = Integer.valueOf(getAppointedTimeIntValue(strStarttime, format));
    }

    if(ymd) {
    intStarttime = Integer.valueOf(getTimesmorning(getDateByTimeStamp(intStarttime.intValue(), format)));
    strStarttime = getDateStringByTimeStamp(intStarttime, format);
    intEndtime = Integer.valueOf(getTimesnight(getDateByTimeStamp(intEndtime.intValue(), format)));
    strEndtime = getDateStringByTimeStamp(intEndtime, format);
    }

    if(intStarttime.intValue() > intEndtime.intValue()) {
    intStarttime = Integer.valueOf(getTimesmorning(getDateByTimeStamp(intEndtime.intValue(), format)));
    strStarttime = getDateStringByTimeStamp(intStarttime, format);
    }

    if(intStarttime.intValue() > intCurrentTime) {
    intStarttime = Integer.valueOf(intTodayTimesmorning);
    strStarttime = getDateStringByTimeStamp(intStarttime, format);
    }

    if(intStarttime.equals(intEndtime)) {
    intStarttime = Integer.valueOf(getTimesmorning(getDateByTimeStamp(intStarttime.intValue(), format)));
    strStarttime = getDateStringByTimeStamp(intStarttime, format);
    }
    }

    timeMap.put("strStarttime", strStarttime);
    timeMap.put("intStarttime", intStarttime);
    timeMap.put("strEndtime", strEndtime);
    timeMap.put("intEndtime", intEndtime);
    return timeMap;
    }
    }

    public static Set<Integer> strTimesToIntTimes(String strTimes, String split, String foratter) {
    HashSet intTimeSet = new HashSet();
    String[] dts = strTimes.split(split);
    String[] var5 = dts;
    int var6 = dts.length;

    for(int var7 = 0; var7 < var6; ++var7) {
    String str = var5[var7];
    if(str != null && !"".equals(str.trim())) {
    try {
    intTimeSet.add(Integer.valueOf(getAppointedTimeIntValue(str.trim(), foratter)));
    } catch (Exception var10) {
    ;
    }
    }
    }

    return intTimeSet;
    }

    public static String foratterIntTime(Integer intTime) {
    if(intTime != null && intTime.intValue() > 0) {
    int h = intTime.intValue() / 60;
    int m = intTime.intValue() % 60;
    String hh = h + "";
    String mm = m + "";
    if(hh.length() == 1) {
    hh = "0" + h;
    }

    if(mm.length() == 1) {
    mm = "0" + m;
    }

    return hh + ":" + mm;
    } else {
    return "00:00";
    }
    }

    public static Integer foratterStrTime(String strTime) {
    String strDateTime = "1970-01-01 " + strTime + ":00";
    Integer result = Integer.valueOf(0);

    try {
    Integer intTime = Integer.valueOf(getAppointedTimeIntValue(strDateTime, "yyyy-MM-dd HH:mm:ss"));
    String hh = getDateStringByTimeStamp(intTime, "HH");
    String mm = getDateStringByTimeStamp(intTime, "mm");
    int h = Integer.parseInt(hh, 10);
    int m = Integer.parseInt(mm);
    result = Integer.valueOf(h * 60 + m);
    } catch (Exception var8) {
    ;
    }

    return result;
    }

    public static Integer foratterDateTime(String strDateTime) {
    Integer resultIntTime = Integer.valueOf(0);
    if(strDateTime == null) {
    return resultIntTime;
    } else {
    strDateTime = strDateTime.trim();
    SimpleDateFormat sdf = new SimpleDateFormat();
    if(!"".equals(strDateTime) && strDateTime.indexOf("date") != 0) {
    if(strDateTime.indexOf("time") == 0) {
    if(strDateTime.length() > 4) {
    switch(strDateTime.toCharArray()[4]) {
    case 'H':
    case 'h':
    sdf = new SimpleDateFormat("HH");
    break;
    case 'M':
    case 'm':
    sdf = new SimpleDateFormat("HH:mm");
    break;
    case 'S':
    sdf = new SimpleDateFormat("HH:mm:ss:SSS");
    break;
    case 's':
    sdf = new SimpleDateFormat("HH:mm:ss");
    }

    strDateTime = sdf.format(new Date());
    } else {
    sdf = new SimpleDateFormat("HH:mm:ss");
    strDateTime = sdf.format(new Date());
    }

    resultIntTime = foratterDateTime(strDateTime);
    } else if(strDateTime.indexOf("datetime") != 0 && strDateTime.indexOf("-") <= 0 && strDateTime.indexOf("/") <= 0) {
    String[] strArrTime = strDateTime.split(":");
    int strArrLenTime = strArrTime.length;
    if(strArrLenTime == 1) {
    resultIntTime = Integer.valueOf(Integer.parseInt(strArrTime[0]));
    } else if(strArrLenTime == 2) {
    resultIntTime = Integer.valueOf(Integer.parseInt(strArrTime[0]) * 60 + Integer.parseInt(strArrTime[1]));
    } else if(strArrLenTime == 3) {
    resultIntTime = Integer.valueOf(Integer.parseInt(strArrTime[0]) * 60 * 60 + Integer.parseInt(strArrTime[1]) * 60 + Integer.parseInt(strArrTime[2]));
    } else {
    resultIntTime = Integer.valueOf(0);
    }
    }
    }

    return resultIntTime;
    }
    }

    public static int getCurrentTimeMinuteValue() {
    Integer result = Integer.valueOf(0);

    try {
    Integer intTime = Integer.valueOf(getCurrentTimeIntValue());
    String hh = getDateStringByTimeStamp(intTime, "HH");
    String mm = getDateStringByTimeStamp(intTime, "mm");
    int h = Integer.parseInt(hh, 10);
    int m = Integer.parseInt(mm);
    result = Integer.valueOf(h * 60 + m);
    } catch (Exception var6) {
    ;
    }

    return result.intValue();
    }

    public static void main(String[] args) {
    int i = getCurrentTimeMinuteValue();
    System.out.println(i);
    System.out.println(foratterIntTime(Integer.valueOf(i)));
    System.out.println(foratterStrTime("11:25"));
    Integer timeint = foratterDateTime("timeM");
    System.out.println(foratterIntTime(timeint));
    }

    public static Date gregorianCalendar(Date date, int value, int field) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    gc.add(field, value);
    date = gc.getTime();
    return date;
    }

    public static Integer addDay(Integer intDate, int day) {
    Integer result = Integer.valueOf(0);
    int oneDay = 86400;
    result = Integer.valueOf(intDate.intValue() + day * oneDay);
    return result;
    }

    public static Integer addDay(Date date, int day) {
    Integer intDate = Integer.valueOf((int)(date.getTime() / 1000L));
    Integer result = Integer.valueOf(0);
    int oneDay = 86400;
    result = Integer.valueOf(intDate.intValue() + day * oneDay);
    return result;
    }

    public static Integer getAgeByStartDate(int startTime) {
    Date startDate = getDateByTimeStamp(startTime, "yyyy-MM-dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(startDate);
    if(cal.before(startCal)) {
    return Integer.valueOf(0);
    } else {
    int year = cal.get(1);
    int month = cal.get(2);
    int day = cal.get(5);
    int startYear = startCal.get(1);
    int startMonth = startCal.get(2);
    int startDay = startCal.get(5);
    int age = year - startYear;
    if(month > startMonth) {
    ++age;
    } else if(month == startMonth && day >= startDay) {
    ++age;
    }

    return Integer.valueOf(age);
    }
    }
    }
  • 相关阅读:
    Spring AOP应用场景你还不知道?这篇一定要看!
    解决 Failed to start LSB: Bring up/down networking 问题
    查出undefined symbol项命令
    将当前目录加入库环境变量
    Fortran代码生成so库
    Java调用Fortran生成so库报“libifport.so.5: 无法打开共享对象文件”错误解决方法
    HBase过滤器(转载)
    HBase设计规范(转载)
    spark(2.1.0) 操作hbase(1.0.2)
    zookeeper搭建
  • 原文地址:https://www.cnblogs.com/duyinqiang/p/7150378.html
Copyright © 2011-2022 走看看