Java将字符串转化为时间格式,与实现日期的计算


将字符串转化为时间格式,具体如下:


有这样一个字符串:“20070911121547”, 
转换成时间格式:2007-09-11 12:15:47 

java代码:

  1. public class bb {  
  2.     public static void main(String[] args) {  
  3.         // TODO Auto-generated method stub      
  4.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");  

  5.         String dateString = "20071128175545";  
  6.         try {  
  7.             Date date = df.parse(dateString);  
  8.             System.out.println(df.format(date));  
  9.         } catch (Exception ex) {  
  10.             System.out.println(ex.getMessage());  
  11.         }  
  12.     }  
  13.   
  14. }  


------------------------------------------------------------------------------------------------------

使用jdk 8 的时间API实现创建新的自定义时间,并此基础上计算,增加(天,月,年),具体见JDK 8时间的使用文章。

java代码:

try { 
      Integer yeah = 2016;
      Integer moon = 8;
      Integer day =15;
      LocalDate dateOfBirth = LocalDate.of(yeah, moon, day);
      LocalDate ldt = dateOfBirth.plusDays(8); 
      System.out.println(ldt);
    } catch (Exception ex) {  
        System.out.println(ex.getMessage());  
    }  



根据上述例子,可以放在一个循环中,实现对两个日期之间所有日期的遍历



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值