java 处理时间的各种方式——获取时间——时间格式化

本文介绍了一个实用的Java时间处理工具类,提供了多种方法来获取和格式化日期时间,包括获取当前时间、未来时间及时间转换等。这些方法简化了日期时间的操作,适用于多种应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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;
     }
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值