yyyyMMddHHmmss格式转为日期

  • Post author:
  • Post category:其他


  	String datetime =  "20140212111012";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    LocalDateTime ldt = LocalDateTime.parse(datetime,dtf);
    System.out.println(ldt);//2014-02-12T11:10:12


    DateTimeFormatter fa = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String datetime2 = ldt.format(fa);
    System.out.println(datetime2);//2014-02-12 11:10:12
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Date->DateString
String format = simpleDateFormat.format(date);
System.out.println(format);//2017-05-16 11:44:05
//DateString->Date
Date parse = simpleDateFormat.parse(format);
System.out.println(parse);//Thu May 16 11:44:05 CST 2017
//Date->long
long time = date.getTime();
System.out.println(time);//1557979623359
//long->DateString
String format1 = simpleDateFormat.format(new Date(Long.valueOf(time)));
System.out.println(format1);//2017-05-16 12:07:03
    //得到年月日的时间戳:不包含时分秒
    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = sdf.format(d);
    Date parse = sdf.parse(format);
    long time = parse.getTime();
    System.out.println(format);
    System.out.println(time);
    String s = "1566403200000";
    String a = "1567395537000";
    Long timeL = Long.valueOf(s);
    Date date = new Date(timeL);
    System.out.println(date);



版权声明:本文为qq_41345773原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。