Simple date formatting giving wrong time

后端 未结 4 1634
暗喜
暗喜 2021-01-20 05:57

I am trying to parse a String to a Date and it giving me right date where as time is wrong.

SimpleDateFormat formatter = new SimpleDateFormat(\"yyyy-MM-dd HH         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 06:26

    Try below code

    SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
    String dateToStr = format.format(new Date());
    System.out.println("dateToStr=>" + dateToStr);
    
    try {
            Date strToDate = format.parse(dateToStr);
            System.out.println("strToDate=>" + strToDate);
    } catch (ParseException e) {
            e.printStackTrace();
    }
    

提交回复
热议问题