• DateUtil


    package com.meiliwan.emall.commons.util;

    import java.io.PrintStream;
    import java.sql.Timestamp;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;

    public class DateUtil
    {
    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static long SECOND_TIME = 1000L;
    public static long MINUTE_TIME = 60L * SECOND_TIME;
    public static long HOUR_TIME = 60L * MINUTE_TIME;
    public static long DAY = 24L * HOUR_TIME;

    private static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("yyyyMM");

    private static final SimpleDateFormat TIME_IDENTITY_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");

    public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String timeAdd(Date date, long diffTime, TimeUnit unit)
    {
    long resultTime = date.getTime() + unit.toMillis(diffTime);

    return getDatetimeStr(resultTime);
    }

    public static String getIdentityTime()
    {
    return TIME_IDENTITY_FORMAT.format(new Date());
    }

    public static String getMonthStr(Date date)
    {
    return MONTH_FORMAT.format(date);
    }

    public static boolean isDate(String date) {
    return date.matches("\d{4}-\d{2}-\d{2}");
    }
    public static String getCurrentDateStr() {
    return getCurrentDateStr("yyyy-MM-dd HH:mm:ss");
    }
    public static String getDatetimeStr(long datetime) {
    return format(new Date(datetime), "yyyy-MM-dd HH:mm:ss");
    }
    public static String getDatetimeStr(Date datetime) {
    return format(datetime, "yyyy-MM-dd HH:mm:ss");
    }
    public static String getDateStr(long datetime) {
    return format(new Date(datetime), "yyyy-MM-dd");
    }

    public static Date getCurrentDate() {
    Calendar cal = Calendar.getInstance();
    Date currDate = cal.getTime();
    return currDate;
    }
    public static Date datetimeStrToDate(String datetimeStr) {
    Date datetime = null;
    try {
    datetime = DATETIME_FORMAT.parse(datetimeStr);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return datetime;
    }

    public static Timestamp getCurrentTimestamp() {
    return new Timestamp(new Date().getTime());
    }

    public static Timestamp getTimestampByDate(Date date) {
    return new Timestamp(date.getTime());
    }

    public static Date getDateByTimestamp(Timestamp timestamp) {
    return new Date(timestamp.getTime());
    }

    public static Timestamp getTimestampByStringYYYYMMddHHmmss(String yyyyMMddHHmmss)
    {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
    date = df.parse(yyyyMMddHHmmss);
    } catch (ParseException e) {
    e.printStackTrace();
    return null;
    }
    return new Timestamp(date.getTime());
    }

    public static boolean isFixTimestamp(String yyyyMMddHHmmss)
    {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
    date = df.parse(yyyyMMddHHmmss);
    } catch (ParseException e) {
    e.printStackTrace();
    return false;
    }
    return true;
    }

    public static String getStringYYYYMMddHHmmssByTimestamp(Timestamp timestamp)
    {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return df.format(new Date(timestamp.getTime()));
    }

    public static boolean isPast(Timestamp start, Timestamp end)
    {
    if (ObjectUtils.isExistNullObj(new Object[] { start, end })) {
    return true;
    }
    return end.getTime() - start.getTime() < 0L;
    }

    public static Date parseStringToDate(String date_str)
    {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
    date = sdf.parse(date_str);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return date;
    }

    public static Date parseStringToDate(String date_str, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = null;
    try {
    date = sdf.parse(date_str);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return date;
    }

    public static String parseLoadTime(long beginTime) {
    long during = System.currentTimeMillis() - beginTime;
    if (during < SECOND_TIME) return during + " millis";

    String loadTimeString = "";
    long minutes = during / MINUTE_TIME;
    long seconds = during / SECOND_TIME;

    if (minutes > 0L) loadTimeString = loadTimeString + minutes + "minutes ";
    if (seconds > 0L) loadTimeString = loadTimeString + seconds + "seconds ";

    return loadTimeString = loadTimeString + during % SECOND_TIME + "millis";
    }

    private static String getCurrentDateStr(String strFormat) {
    Calendar cal = Calendar.getInstance();
    Date currDate = cal.getTime();
    return format(currDate, strFormat);
    }

    private static String format(Date aTs_Datetime, String as_Pattern) {
    if ((aTs_Datetime == null) || (as_Pattern == null)) {
    return null;
    }
    SimpleDateFormat dateFromat = new SimpleDateFormat(as_Pattern);
    return dateFromat.format(aTs_Datetime);
    }

    public static int todayNowTime()
    {
    Calendar today = Calendar.getInstance();
    today.setTimeInMillis(System.currentTimeMillis());
    int hour = today.get(11);
    int minute = today.get(12);
    int second = today.get(13);
    today.set(11, 0);
    today.set(12, 0);
    today.set(13, 0);
    long beginTime = today.getTimeInMillis() / 1000L;
    today.set(11, hour);
    today.set(12, minute);
    today.set(13, second);
    long endTime = today.getTimeInMillis() / 1000L;
    return (int)(endTime - beginTime + 1L);
    }

    public static void main(String[] args)
    {
    System.out.println(getCurrentDateStr());
    System.out.println(getDatetimeStr(System.currentTimeMillis()));
    System.out.println(getCurrentDateStr("yyyy.MM.dd HH:mm:ss"));
    System.out.println(format(new Date(System.currentTimeMillis()), "yyyy.MM.dd HH:mm:ss"));
    }
    }

  • 相关阅读:
    TreeSelect组件:vmodel语法糖进行父子组件传值案例
    带 icon 的输入框:slot方式。slot="prefix"和slot="suffix"
    双向绑定vmodel与单向绑定vbind:value
    使用Set集合对List集合去重
    前端报错: error in ./src/assets/fonts/iconfont.svg?t=1523541245904,Module parse failed: Unexpected token (1:0),vue.config.js中引入chainWebpack后报错
    当主键不是id时,而是其他字段,那么该字段要加上@Id注解(除了表中指定主键,实体类中也要用@Id指定主键)
    java将文件转为base64字符串和将base64字符串转为文件
    父组件向子组件传递getList方法:provide/inject
    带卡片的input输入框
    C#中使用#if DEBUG
  • 原文地址:https://www.cnblogs.com/blogszixin/p/3461521.html
Copyright © 2020-2023  润新知