java 处理时间的各种方式——获取时间——时间格式化详解编程语言

TimeUtil.java

package com.snow; 
 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
public class TimeUtil { 
     
    /** 
     *         获取当前时间    格式为    2016-06-16 10:32:53 
     *  
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:27 
     */ 
    public static String getCurrentTimeSceond() { 
          
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
          String time=format.format(new Date()); 
          return time; 
     } 
     
    /** 
     *         获取当前时间     加10分钟        格式为    2016-06-16 10:42:53 
     *  
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:32 
     */ 
    public static String getAfterTenTimeSceond() { 
          
         long currentTime = System.currentTimeMillis() + 10 * 60 * 1000; 
         Date date = new Date(currentTime); 
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
         String nowTime=df.format(date); 
         return nowTime; 
     } 
     
    /** 
     *         获取当前时间的   时分   格式为  2016-06-16 10:32 
     *  
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:39 
     */ 
    public static String getCurrentTimeMinute() { 
          
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
          String time=format.format(new Date()); 
          return time; 
     } 
    /** 
     *         获取当前时间        年月        格式为    2016-06-16 
     *  
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:47 
     */ 
    public static String getCurrentTimeDay() { 
          
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
          String time=format.format(new Date()); 
          return time; 
    } 
     
    /** 
     *         获取时间,格式为        201606161032053 
     *  
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:34:09 
     */ 
    public static String getuniqukey() { 
          
         DateFormat format = new SimpleDateFormat("yyyyMMddHHmmsss"); 
          String time=format.format(new Date()); 
          return time; 
     } 
     
     
    /** 
     *         将        Date   转换为  时间格式  格式为     yyyy-MM-dd HH:mm:ss 
     *  
     * @param date 
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:50 
     */ 
    public static String convertTimeSceond(Date date) { 
          
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
          String time=format.format(date); 
          return time; 
     } 
     
    /** 
     *         将        Date   转换为  时间格式  格式为     yyyy-MM-dd 
     *  
     * @param date 
     * @return String 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:54 
     */ 
    public static String convertTimeDay(Date date) { 
          
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
          String time=format.format(date); 
          return time; 
    } 
     
    /** 
     *         将        String格式的时间    转换为  时间格式  格式为     Thu Jun 16 10:52:53 CST 2016 
     *  
     * @param time 
     * @return Date 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:33:57 
     */ 
    public static Date convertDateSceond(String time) { 
          
         DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
         Date date=null; 
            try { 
                date = format.parse(time); 
            } catch (ParseException e) { 
                e.printStackTrace(); 
            } 
             return date; 
     } 
     
    /** 
     *         将        String格式的时间   转换为  时间格式  格式为     Thu Jun 16 00:00:00 CST 2016 
     *  
     * @param time 
     * @return Date 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:34:01 
     */ 
    public static Date convertDateDay(String time) { 
          
          DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
          Date date=null; 
        try { 
            date = format.parse(time); 
        } catch (ParseException e) { 
            e.printStackTrace(); 
        } 
         return date; 
    } 
     
    /** 
     *         判断    第一个时间是否大于第二个时间   false 
     *  
     * @param date1 
     * @param date2 
     * @return boolean 
     * @author jingxue.chen 
     * @date 2016-6-16 上午10:34:05 
     */ 
    public static boolean compDate(String date1,String date2) { 
          
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        boolean time=false; 
        try { 
            Date dates1 = format.parse(date1); 
            Date dates2 = format.parse(date2); 
            if(dates1.getTime()>dates2.getTime()){ 
                time=true; 
            } 
        } catch (ParseException e) { 
            e.printStackTrace(); 
        } 
        return time; 
     } 
     
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/19497.html

(0)
上一篇 2021年7月19日 22:15
下一篇 2021年7月19日 22:15

相关推荐

发表回复

登录后才能评论