java常用 日期时间格式的转化,以及时间的计算

时间类型的转化:  

常用是时间: 
问题一: 时间格式转化问题: 
时间格式转化为字符串:

 就是时间的显示,以及显示格式的问题: 

    1.字符串专换为Date
     

   SimpleDateFormat format = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss_SSS);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
        }
        return date;



     2. 日期格式转换为字符串
   

  SimpleDateFormat format = new SimpleDateFormat(yyyy_MM_dd);
        String str = format.format(date);
        return str;



问题二:时间计算问题: 
 时间计算: 某个时间, 几天后的一个日期: 
 某一天,几天后 ,的日期. 

关于时间的计算问题同一转化为毫秒值进行计算.
1. 日期加time 时间

 public static Date getDataByDateAndTime(Date date, Time time) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.set(Calendar.HOUR,time.getHours());
        calendar.set(Calendar.MINUTE,time.getMinutes());
        calendar.set(Calendar.SECOND,time.getSeconds());
        return calendar.getTime();  
     }


 其实, Time 类是Date类的一个子类;  

Time的毫秒值 也是某一刻到 1970 年的毫秒值, 只是舍弃了日期部分.

 
2.  @描述:获取多少小时以后的时间
    
 

     public static Time getAfterTimeHour(Time time,double h){ 
          Time newTime=new Time((long) (time.getTime()+h*60*60*1000));
          return newTime;
      }



     @描述:获取多少小时以后的时间
    
  

   public static Date getAfterDateHour(Date date,double h){ 
         return new Date((long) (date.getTime()+h*60*60*1000));
     }

各种转化:

https://blog.csdn.net/jingsong2015/article/details/77714780

java时间格式大全(法源码) java,date,时间,时间格式,法源码 * * @return */ public static String getTimeShort() { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); Date currentTime = new Date(); String dateString = formatter.format(currentTime); return dateString; } /** * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss * * @param strDate * @return */ public static Date strToDateLong(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } /** * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss * * @param dateDate * @return */ public static String dateToStrLong(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(dateDate); return dateString; } /** * 将短时间格式时间转换为字符串 yyyy-MM-dd * * @param dateDate * @param k * @return */ public static String dateToStr(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(dateDate); return dateString; } /** * 将短时间格式字符串转换为时间 yyyy-MM-dd * * @param strDate * @return */ public static Date strToDate(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } /** * 得到现在时间 * * @return */ public static Date getNow() { Date currentTime = new Date(); return currentTime; } /** * 提取一个月中的最后一天 * * @param day * @return */ public static Date getLastDate(long day) { Date date = new Date(); long date_3_hm = date.getTime() - 3600000 * 34 * day; Date date_3_hm_date = new Date(date_3_hm); return date_3_hm_date; } /** *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值