【java工具类】timeutil

本文介绍了一个Java日期时间操作工具类,该类提供了一系列方法用于处理日期和时间的格式化、获取当前时间、计算时间差等。文章还展示了如何使用这些方法来解析字符串为日期类型、获取指定日期的时间戳、计算两个日期之间的差值等。

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

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

 

public class TimeUtil {
    //private static Calendar  calCurrent;
    //private static    Date date;
    public static String getTime(){
        return returnString("yyyy-MM-dd HH:mm:ss");
    }
    public static String getYearMDE(){
          return returnString("yyyy-MM-dd E");
    }
    public static String getYearMD(){
          return returnString("yyyy-MM-dd");
    }
    public static String getTimeString(){
        return returnString("yyyyMMddHHmmssSSSS");
    }
    public static String getWeekbyTimes(String str){
        Calendar c=Calendar.getInstance();
        SimpleDateFormat sdf  =   new  SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(str);
            c.setTime(date);
            int ltime = c.get(Calendar.DAY_OF_WEEK);
            return ltime+"";
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
       
    }
    public static String getFristDayInWeekbyTimes(String str){
        Calendar c=Calendar.getInstance();
        SimpleDateFormat sdf  =   new  SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(str);
            c.setTime(date);
            c.setFirstDayOfWeek(Calendar.MONDAY);
            c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek());
            Date d = c.getTime();
            String ltime =  sdf.format(d);
            return ltime;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
    public static String dateDiff(String startTime, String endTime, String format,String type) {
    //按照传入的格式生成一个simpledateformate对象
    SimpleDateFormat sd = new SimpleDateFormat(format);
    long nd = 1000*24*60*60;//一天的毫秒数
    long nh = 1000*60*60;//一小时的毫秒数
    long nm = 1000*60;//一分钟的毫秒数
    long ns = 1000;//一秒钟的毫秒数
    long diff;
    long day = 0;
    try {
        //获得两个时间的毫秒时间差异
        diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        if (type.equals("1")) {
            day = diff/nd;//计算差多少天
        }else if(type.equals("2")){
            day = diff%nd/nh;//计算差多少小时
        }else if(type.equals("3")){
            day = diff%nd%nh/nm;//计算差多少分钟
        }else if(type.equals("4")){
            day = diff%nd%nh%nm/ns;//计算差多少秒
        }
       
        //输出结果
    } catch (ParseException e) {
         e.printStackTrace();
    }
    return day+"";

}
    static{
       
    }
    public static String returnString(String TimeStyle){
        SimpleDateFormat sdf  =   new  SimpleDateFormat(TimeStyle);
        Calendar  calCurrent = Calendar.getInstance();
        Date date = calCurrent.getTime();
          return sdf.format(date);
    }
    public static boolean leap_year(int year){
        boolean result = false;
        if((year%4 == 0&&year%100!=0)||(year%400==0)){
            result = true;
        }
        return result;
    }
    public static String[] allMonth(int year){
        int feb = ((year%4==0&&year%100!=0)||year%400==0)?29:28;
        String[] m_days = {"31",feb+"","31","30","31","30","31","31","30","31","30","31"};
        return m_days;
    }
    
    public static void main(String[] arg){
        //System.out.println(getWeekbyTimes("2012-04-11"));
        //System.out.println(getFristDayInWeekbyTimes("2012-04-11"));
        new Thread(new Runnable() {
            public void run() {
                long begin = System.currentTimeMillis();//取开始时间 单位是毫秒
                for(long i = 0;i<10000000;i++){
                    getYearMD();
                }
                long end = System.currentTimeMillis();//取开始时间 单位是毫秒
                System.out.println("一共运行了" + (end - begin) + "毫秒");
            }
        }).start();
    }
}


获取当天日期 获取本周一日期 获取本周日的日期 获取上周一日期:" + tt.getPreviousWeekday("yyyy-MM-dd")); 获取上周日日期:" + tt.getPreviousWeekSunday("yyyy-MM-dd")); 获取上周一日期:" + tt.getWeekday(-1, "yyyy-MM-dd")); 获取上周日日期:" + tt.getWeekSunday(-1, "yyyy-MM-dd")); 获取下周一日期:" + tt.getNextMonday("yyyy-MM-dd")); 获取下周日日期:" + tt.getNextSunday("yyyy-MM-dd")); 获取本月第一天日期:" + tt.getFirstDayOfMonth()); 获取本月最后一天日期:" + tt.getDefaultDay()); 获取上月第一天日期:" + tt.getPreviousMonthFirst("yyyy-MM-dd")); 获取上月最后一天的日期:" + tt.getPreviousMonthEnd("yyyy-MM-dd")); 获取某月第一天日期:" + tt.getMonthFirst(0, "yyyy-MM-dd")); 获取某月最后一天的日期:" + tt.getMonthEnd(0, "yyyy-MM-dd")); 获取下月第一天日期:" + tt.getNextMonthFirst("yyyy-MM-dd")); 获取下月最后一天日期:" + tt.getNextMonthEnd("yyyy-MM-dd")); 获取本年的第一天日期:" + tt.getCurrentYearFirst()); 获取本年最后一天日期:" + tt.getCurrentYearEnd()); 获取去年的第一天日期:" + tt.getPreviousYearFirst()); 获取去年的最后一天日期:" + tt.getPreviousYearEnd()); 获取明年第一天日期:" + tt.getNextYearFirst()); 获取明年最后一天日期:" + tt.getNextYearEnd()); 获取本季度第一天:" + tt.getThisSeasonFirstTime(11)); 获取本季度最后一天:" + tt.getThisSeasonFinallyTime(11)); 获取两个日期之间间隔天数 获取当前月的第几周:" + tt.getWeekOfMonth()); 获取当前年份:" + tt.getYear()); 获取当前月份:" + tt.getMonth()); 获取今天在本年的第几天:" + tt.getDayOfYear()); 获得今天在本月的第几天(获得当前日):" + tt.getDayOfMonth()); 获得今天在本周的第几天:" + tt.getDayOfWeek()); 字符串转时间 获得一个日期所在周的星期几的日期
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值