简体   繁体   中英

Converting the format of the date in java

I wanted to create a Date object in "yyyy-MM-dd hh:MM:ss" format. Below is the code I am using. date object formed after parsing the string is not the required format(expected:2020-04-04 00:00:00 but was: Sat Apr 04 00:00:00 CDT 2020). How can I convert the date to required format

  Date da = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dt = "2020-04-04 00:00:00";
  try {
    da = sdf.parse(dt);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I wanted to create a Date object in "yyyy-MM-dd hh:MM:ss" format. Below is the code I am using. date object formed after parsing the string is not the required format(expected:2020-04-04 00:00:00 but was: Sat Apr 04 00:00:00 CDT 2020). How can I convert the date to required format

  Date da = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String dt = "2020-04-04 00:00:00";
  try {
    da = sdf.parse(dt);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM