Java时间工具类,总有一款你用的到

public class TimeUtil
{
    /**
     * 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S
     */
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";

    /**
     *  时间戳 转化为 星期几
     */
    public static String stampToWeek(long timeStamp)
    {
        int mydate = 0;
        String week = null;
        Calendar cd = Calendar.getInstance();
        cd.setTime(stampToTime(timeStamp));
        mydate = cd.get(Calendar.DAY_OF_WEEK);
        // 获取指定日期转换成星期几
        if (mydate == 1) {
            week = "周日";
        } else if (mydate == 2) {
            week = "周一";
        } else if (mydate == 3) {
            week = "周二";
        } else if (mydate == 4) {
            week = "周三";
        } else if (mydate == 5) {
            week = "周四";
        } else if (mydate == 6) {
            week = "周五";
        } else if (mydate == 7) {
            week = "周六";
        }
        return week;
    }

    /**
     *  时间 转换为 时间戳
     */
    public static long timeToStamp(Date date)
    {
        long ts = date.getTime()/1000L;
        return ts;
    }

    /**
     *  时间戳 转换为 时间
     */
    public static Date stampToTime(long timeStamp)
    {
        Date date = new Date(Long.parseLong(String.valueOf(timeStamp*1000L)));
        return date;
    }

    /**
     *  获取系统当前时间戳
     */
    public static String getSystemTimeStamp()
    {

        long timeStampSec = System.currentTimeMillis()/1000;
        String timestamp = String.format("%010d", timeStampSec);

        return timestamp;
    }

    /*分别获取 时间的 年月日*/
    public static int getTimeDetail(long stamp,String type)
    {
        Date date = stampToTime(stamp);

        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(date);//设置当前日期

        int year = calendar.get(Calendar.YEAR);//获取年份
        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
        int day = calendar.get(Calendar.DATE);//获取日
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//获取小时
        int minute = calendar.get(Calendar.MINUTE);//获取分钟
        int second  = calendar.get(Calendar.SECOND);//获取秒

        if(type.equals("day"))
        {
            return day;
        }
        else if(type.equals("month"))
        {
            return month;
        }
        else if(type.equals("year"))
        {
            return year;
        }
        else if(type.equals("hour"))
        {
            return hour;
        }
        else if(type.equals("minute"))
        {
            return minute;
        }
        else if(type.equals("second"))
        {
            return second;
        }
        else
        {
            return 0;
        }
    }

    /**
     *   字符串 转化为 时间
     */
    public static Date strToDate(String str)
    {
        SimpleDateFormat sdf =  new SimpleDateFormat( "yyyy-MM-dd HH-mm-ss" );
        Date date;
        try
        {
            date = sdf.parse(str);
            return date;
        }
        catch (ParseException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *   时间 转化为 字符串
     */
    public static String dateToStr(Date date)
    {
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        String str = sdf.format(date);
        return str;
    }

    /**
     * 当秒数大于60秒时 转为 分钟和秒
     */
    public static String sed2sedminu(int seconds)
    {
        if(seconds >= 60)
        {
            int minutes = seconds / 60;
            int remainingSeconds = seconds % 60;

            if(minutes > 9)
            {
                if(remainingSeconds>9)
                {
                    return minutes+":"+remainingSeconds;
                }
                else
                {
                    return minutes+":0"+remainingSeconds;
                }
            }
            else
            {
                if(remainingSeconds>9)
                {
                    return "0"+minutes+":"+remainingSeconds;
                }
                else
                {
                    return "0"+minutes+":0"+remainingSeconds;
                }
            }

        }
        else
        {
            if(seconds > 9)
            {
                return "00:"+seconds;
            }
            else
            {
                return "00:0"+seconds;
            }
        }
    }

    /**
     * 获取日期年份
     *
     * @param stamp 时间戳
     *
     * @return 年
     */
    public static String getYear(long stamp)
    {
        Date date = stampToTime(stamp);
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        return df.format(date).substring(0, 4);
    }
    /**
     * 功能描述:返回月
     *
     * @param stamp 时间戳
     *
     * @return 返回月份
     */
    public static int getMonth(long stamp)
    {
        Date date = stampToTime(stamp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 功能描述:返回日
     *
     * @param stamp 时间戳
     *
     * @return 返回日份
     */
    public static int getDay(long stamp)
    {
        Date date = stampToTime(stamp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 功能描述:返回小时
     *
     * @param stamp 时间戳
     *
     * @return 返回小时
     */
    public static int getHour(long stamp)
    {
        Date date = stampToTime(stamp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述:返回分
     *
     * @param stamp 时间戳
     *
     * @return 返回分钟
     */
    public static int getMinute(long stamp)
    {
        Date date = stampToTime(stamp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 返回秒钟
     *
     * @param stamp 时间戳
     *
     * @return 返回秒钟
     */
    public static int getSecond(long stamp)
    {
        Date date = stampToTime(stamp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 功能描述:返回毫
     *
     * @param stamp  时间戳
     *
     * @return 返回毫秒
     */
    public static long getMillis(long stamp)
    {
        Date date = stampToTime(stamp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }
}
/** * 字符串 转化为 时间 */ 
public static Date strToDate(String str) 
{ 
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
    Date date; 
    try { date = sdf.parse(str); return date; } 
    catch (ParseException e) { e.printStackTrace(); return null; }
 } /** * 时间 转化为 字符串 */ public static String dateToStr(Date date) { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
String str = sdf.format(new Date()); return str; }}

猜你喜欢

转载自blog.csdn.net/qq_30555429/article/details/79941065
今日推荐