简体   繁体   中英

Conversion of String with timezone to date

I have a string containing date with timezone like 2014-01-24 09.05.14 -06:00.

I need to convert this into date format as YYYYMMDD. What is the best way in Java to convert the above string into date.

Do it like this (requires JDK 7+):

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss X");
DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = "2014-01-24 09.05.14 -06:00";
Date date = inputFormat.parse(dateString);
System.out.println(outputFormat.format(date));

One way of doing it is to convert the String into a Date and then use a DateFormatter (eg the SimpleDateFormat ) to formate that date.

Take a look at this Tutorial for further information.

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