• 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"));
    }
    }

  • 相关阅读:
    我不为人人,人人不为我
    sed 小结
    linux 之 压缩 / 解压
    java arraylist的问题
    flex swf和movieclip之前的微妙关系
    Flex contextMenu
    。。
    数据库
    flex Vector
    浮动ip
  • 原文地址:https://www.cnblogs.com/blogszixin/p/3461521.html
Copyright © 2020-2023  润新知