Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.joda.time.DateTime;
- import org.joda.time.format.DateTimeFormat;
- import org.joda.time.format.DateTimeFormatter;
- import java.time.LocalDateTime;
- import java.time.OffsetDateTime;
- import java.time.ZoneOffset;
- public class Utctime2Datetime {
- public static void main(String[] args) throws Exception {
- String ISODateTime = "2018-08-07T14:54:18.183+08:00";
- String dateStr = Utctime2Datetime.getDateStrFromISO8601Timestamp(ISODateTime);
- System.out.println("dateStr = " + dateStr);
- String dateTime = "2020-09-07 08:44:28";
- String ISOStr = Utctime2Datetime.getISO8601TimestampFromDateStr(dateTime);
- System.out.println("ISOStr = " + ISOStr);
- }
- public static String getDateStrFromISO8601Timestamp(String ISOdate){
- DateTimeFormatter dtf1 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
- DateTime dt= dtf1.parseDateTime(ISOdate);
- DateTimeFormatter dtf2= DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
- return dt.toString(dtf2);
- }
- public static String getISO8601TimestampFromDateStr(String timestamp){
- java.time.format.DateTimeFormatter dtf1 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- LocalDateTime ldt = LocalDateTime.parse(timestamp,dtf1);
- ZoneOffset offset = ZoneOffset.of("+08:00");
- OffsetDateTime date = OffsetDateTime.of(ldt ,offset);
- java.time.format.DateTimeFormatter dtf2 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
- return date.format(dtf2 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement