JAVA common time tool class

Don't say it, just look at the code


package com.poly.rbl.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Time tool class
 *
 * @author gogym
 * @version August 30, 2017
 * @see DateTimeUtil
 * @since
 */
public class DateTimeUtil
{

    /**
     * Get the current system time
     *
     * @return
     * @author gogym
     * @date 2016-4-28 10:07:54 AM
     * @comment
     */
    public static String getCurrentTime()
    {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Set date format
        String time = df.format(new Date());// new Date() is to get the current system time

        return time;
    }

    /**
     * Get the current system time
     *
     * @return
     * @author gogym
     * @date 2016-4-28 10:07:54 AM
     * @comment
     */
    public static Long getCurrentLongTime()
    {

        Long time = new Date().getTime();// new Date() is to get the current system time
        return time;
    }

    /**
     * date type to String type
     *
     * @param date
     * @return
     * @author gogym
     * @date 2016-4-28 10:10:25 AM
     * @comment
     */
    public static String convertDateToString(Date date)
    {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Set date format

        return df.format(date);

    }

    /**
     * long type to String type
     *
     * @param date
     * @return
     * @author gogym
     * @date 2016-4-28 10:10:25 AM
     * @comment
     */
    public static String convertLongToString(Long date)
    {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Set date format

        return df.format(new Date(date));

    }

    /**
     * String type to date type
     *
     * @param date
     * @return
     * @author gogym
     * @date 2016-4-28 10:10:25 AM
     * @comment
     */
    public static Date convertStringToDate(String time)
    {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Set date format

        try
        {
            return df.parse(time);
        }
        catch (ParseException e)
        {
            e.printStackTrace ();
        }
        return null;

    }

    /**
     * Get the day before the specified time
     *
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay)
    {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try
        {
            date = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(specifiedDay);
        }
        catch (ParseException e)
        {
            e.printStackTrace ();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        return dayBefore;
    }

    /**
     * Get the day after the specified time
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay)
    {
        Calendar c = Calendar.getInstance();
        Date date = null;

        try
        {
            date = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(specifiedDay);
        }
        catch (ParseException e)
        {
            e.printStackTrace ();
        }

        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        return dayAfter;
    }

    /**
     * Get the day before the specified time
     *
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(Date specifiedDay)
    {
        Calendar c = Calendar.getInstance();
        Date date = specifiedDay;
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        return dayBefore;
    }

    /**
     * Get the day after the specified time
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(Date specifiedDay)
    {
        Calendar c = Calendar.getInstance();
        Date date = specifiedDay;

        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        return dayAfter;
    }

    /**
     * Get the time at 0:00 of the day
     *
     * @authorgj
     * @date: 2017/3/10
     * @time: 12:29
     **/
    public static Long getTimesmorning()
    {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * Get the day of the week
     *
     * @authorgj
     * @date: 2017/3/10
     * @time: 14:19
     **/
    public static String getWeekOfDate(Date dt)
    {
        String[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) w = 0;
        return weekDays[w];
    }

    /**
     * Get the time point, similar to WeChat
     *
     * @authorgj
     * @date: 2017/3/10
     * @time: 14:49
     **/
    public static String getTimePoint(String time)
    {
        Long Tmp = Long.valueOf(time);
        return getTimePoint(Tmp);
    }

    public static String getTimePoint(Long time)
    {
        // current time
        Long now = new Date().getTime();
        DateFormat df;
        int day = (60 * 60 * 24) * 1000;
        String pointText = "1970-01-01";

        // The time point is earlier than zero of the day
        if (time <= now && time != null)
        {
            Date date = new Date(time);
            if (time < getTimesmorning())
            {
                if (time >= getTimesmorning() - day)
                {// later than zero o'clock yesterday
                    pointText = "Yesterday";
                    return pointText;
                }
                else
                {// earlier than zero o'clock yesterday
                    if (time >= getTimesmorning() - 6 * day)
                    {// It is later than zero seven days ago, showing the day of the week
                        return getWeekOfDate(date);
                    }
                    else
                    {// Display the specific date
                        df = new SimpleDateFormat("yyyy-MM-dd");
                        pointText = df.format(date);
                        return pointText;
                    }
                }
            }
            else
            {// No date and time, specific time within the day
                df = new SimpleDateFormat("HH:mm");
                pointText = df.format(date);
                return pointText;
            }
        }
        return pointText;
    }

    /**
     * Get time interval prompts, similar to Weibo
     *
     * @authorgj
     * @date: 2017/6/12
     * @time: 13:08
     **/
    public static String getInterval(Long t)
    {
        String interval = null;
        // Subtract the time interval d1.getTime() from the previous time to 1970 from the time interval new Date().getTime() from the current time to 1970 to get the time interval between the previous time and the current time
        long time = new Date().getTime() - t;// The resulting time interval is milliseconds

        if (time / 1000 < 10 && time / 1000 >= 0)
        {
            // If the time interval is less than 10 seconds, the unit of the time interval obtained by "just" time/10 is displayed in seconds
            interval = "Just now";

        }
        else if (time / 1000 < 60 && time / 1000 > 0)
        {
            // If the time interval is less than 60 seconds, display how many seconds ago
            int se = (int)((time % 60000) / 1000);
            interval = se + "seconds ago";

        }
        else if (time / 60000 < 60 && time / 60000 > 0)
        {
            // If the time interval is less than 60 minutes, display how many minutes ago
            int m = (int)((time % 3600000) / 60000);// The unit of the time interval is minutes
            interval = m + "minutes ago";

        }
        else if (time / 3600000 < 24 && time / 3600000 >= 0)
        {
            // If the time interval is less than 24 hours, display how many hours ago
            int h = (int)(time / 3600000);// The unit of the time interval is hours
            interval = h + "hours ago";

        }
        else if (time / 3600000 / 24 < 30 && time / 3600000 / 24 >= 0)
        {
            // if time is less than 30 days
            int h = (int)(time / 3600000 / 24);
            interval = h + "days ago";
        }
        else
        {
            // If it is greater than 30 days, the normal time is displayed, but the seconds are not displayed
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            ParsePosition pos2 = new ParsePosition (0);
            // Date d2 = (Date) sdf.parse(creattetime, pos2);
            Date d2 = new Date(t);
            interval = sdf.format(d2);
        }
        return interval;

    }

    public static String getInterval(String createtime)
    { // The incoming time format must be similar to 2012-8-21 17:53:20
        String interval = null;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition (0);
        Date d1 = (Date)sd.parse(createtime, pos);
        return getInterval(d1.getTime());
    }

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325405653&siteId=291194637