Parsing error for date field

前端 未结 2 1377
独厮守ぢ
独厮守ぢ 2020-12-12 05:40

I want to parse a date in YYYY-MM-DD format to YYYYMMDD. If I use the following function, it returns me a YYYYMMDD format but with a d

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 06:13

    A SimpleDateFormat should be capable of achieving what you're after. Be very careful with the format marks, D and d mean different things

    String oldDateString = "2013-05-16";
    System.out.println(oldDateString );
    Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldDateString);
    System.out.println(date);
    String newDateString = new SimpleDateFormat("yyyyMMdd").format(date);
    System.out.println(newDateString);
    

    (Also, beware of Y and y :P)

    This outputs

    2013-05-16
    Thu May 16 00:00:00 EST 2013
    20130516
    

    For me...

提交回复
热议问题