java 时间类工具 DateFormatUtil

类名[DateFormatUtil]

package com.demo.util

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtil {  //正文开始
//------------------------------------------------------------------------当前时间处理
    /**
     * 将当前时间转成yyyyMMddHHmmssSSS格式
     */
    public synchronized  static String getMilli() {
        SimpleDateFormat  sdf= new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date date = new Date();
        try {
            Thread.sleep(1);
        } catch (Exception e) {

        }
        return sdf.format(date);
    }
    
  /**
     * 将当前时间转成yyyyMMddHHmmss格式
     */
    public static String getsecond() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        return sdf.format(date);
    }

 /**
     * 将当前时间转成yyyy-MM-dd HH:mm:ss格式
     */
    public static String getTimesecond() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return sdf.format(date);
    }

 /**
     * 将当前时间转成yyyy-MM-dd 格式
     */
    public static String getTimeDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return sdf.format(date);
    }



//----------------------------------------指定时间处理
  /**
     * 将指定时间转成yyyy-MM-dd 格式
     */
    public static String getTimeDayByString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }


   /**
     * 将指定时间转成yyyy-MM-dd HH:mm:ss 格式
     */
    public static String getTimeDayByStringDou(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }


    /**
     * 获取传入时间的 最早时间 00:00:00
     */
    public static Date getDateTop(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.AM_PM, 0);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date nextDate = cal.getTime();
        return nextDate;
    }


  /**
     * 获取传入时间的 最晚时间 23:59:59
     */
    public static Date getDateLast(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.AM_PM, 0);
        cal.set(Calendar.HOUR, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        Date nextDate = cal.getTime();
        return nextDate;
    }

 /**
     * 时间字符串转时间
     * @param str
     * @return
     */
    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            date=new Date();
        }
        return date;
    }

    /**
     * 传入时间减1天
     * */
    public static Date getStringBeforeDay(Date datestr){
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(datestr);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        Date date = calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        return date;
    }

     * 获取提前某分钟的时间 正数是加时间  负数是减时间
     */
    public static String getTimeNEWDayMinute(Integer minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, minute);
        return sdf.format(nowTime.getTime());
    }

   /**
     * 时间字符串转时间
     * @param str
     * @return
     */
    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            date=new Date();
        }
        return date;
    }
    /**
    



// 正文end
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值