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

来源:互联网 发布:vue.js 显示隐藏 编辑:程序博客网 时间:2024/05/24 04:13


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


有这样一个字符串:“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());      }  



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



0 0
原创粉丝点击