博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于java时间转换及计算的整理
阅读量:6998 次
发布时间:2019-06-27

本文共 3215 字,大约阅读时间需要 10 分钟。

 一直都想弄个自己的博客来秀一下,也是想记录一些工作的点点滴滴,今天开始自己的博客生涯,希望自己能够坚持下去,加油!话不多说,接触java不久,最近多次遇到时间的转换和计算,今天在此做个总结:

1.Date类和Calender类

       Date用于记录某一个含日期的、精确到毫秒的时间。重点在代表一刹那的时间本身。

  Calendar用于将某一日期放到历法中的互动——时间和年、月、日、星期、上午、下午、夏令时等这些历法规定互相作用关系和互动。Calendar本身代表公历的一个简化缩水版。

Date类常用方法:   

Date date0 = new Date();Date date1 = new Date(time);

通过get方法可以获取具体的年、月、日、时间戳等信息。注意getYear()方法得到的并不是获取当前年份,而是获取到和1900年的差值。

Calender类常用方法:

Calendar calendar = Calendar.getInstance();Date date = calendar.getTime();

 通过Calendar 方法获取一个Date 的实例。

Calendar 方法获取年月日的方法:

int year =calendar.get(Calendar.YEAR);        int month=calendar.get(Calendar.MONTH)+1;        int day =calendar.get(Calendar.DAY_OF_MONTH);        int hour =calendar.get(Calendar.HOUR_OF_DAY);        int minute =calendar.get(Calendar.MINUTE);        int seconds =calendar.get(Calendar.SECOND);

2.时间与时间戳相互转换

时间转换为时间戳:

/*      * 将时间转换为时间戳     */        public static String dateToStamp(String s) throws ParseException{        String res;        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = simpleDateFormat.parse(s);        long ts = date.getTime();        res = String.valueOf(ts);        return res;    }

时间戳转换为时间:

/*      * 将时间戳转换为时间     */    public static String stampToDate(String s){        String res;        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        long lt = new Long(s);        Date date = new Date(lt);        res = simpleDateFormat.format(date);        return res;    }

3.关于时间的计算

   3.1获取当前时间字符串

/**  * 获取现在时间  *   * @return返回字符串格式 yyyy-MM-dd HH:mm:ss  */ public static String getStringDate() {  Date currentTime = new Date();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  String dateString = formatter.format(currentTime);  return dateString; }

   3.2给定一个日期型字符串,返回加减n天后的日期型字符串  

 public String nDaysAfterOneDateString(String basicDate,int n)   {             SimpleDateFormat df   =   new   SimpleDateFormat("yyyy-MM-dd");             Date tmpDate   =   null;             try   {                 tmpDate = df.parse(basicDate);             }             catch(Exception   e){                 //   日期型字符串格式错误             }             long nDay=(tmpDate.getTime()/(24*60*60*1000)+1+n)*(24*60*60*1000);             tmpDate.setTime(nDay);                 return df.format(tmpDate);         }  

    3.3 给定一个日期,返回加减n天后的日期  

   public Date nDaysAfterOneDate(Date   basicDate,int   n)   {             long nDay=(basicDate.getTime()/(24*60*60*1000)+1+n)*(24*60*60*1000);             basicDate.setTime(nDay);           return basicDate;         }  

    3.4计算两个日期相隔的天数  

      public int nDaysBetweenTwoDate(String firstString,String secondString)   {             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");             Date firstDate=null;             Date secondDate=null;             try {                 firstDate = df.parse(firstString);                 secondDate=df.parse(secondString);             }             catch(Exception   e)   {                 //   日期型字符串格式错误             }                 int nDay=(int)((secondDate.getTime()-firstDate.getTime())/(24*60*60*1000));             return nDay;         }  

 

 

 

转载于:https://www.cnblogs.com/joyzk/p/7259402.html

你可能感兴趣的文章