Advertisement
hivefans

isotime2datetimestr

Mar 17th, 2021
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import org.joda.time.DateTime;
  2. import org.joda.time.format.DateTimeFormat;
  3. import org.joda.time.format.DateTimeFormatter;
  4.  
  5. import java.time.LocalDateTime;
  6. import java.time.OffsetDateTime;
  7. import java.time.ZoneOffset;
  8.  
  9.  
  10. public class Utctime2Datetime {
  11.     public static void main(String[] args) throws Exception {
  12.         String ISODateTime = "2018-08-07T14:54:18.183+08:00";
  13.         String dateStr = Utctime2Datetime.getDateStrFromISO8601Timestamp(ISODateTime);
  14.         System.out.println("dateStr = " + dateStr);
  15.  
  16.         String dateTime = "2020-09-07 08:44:28";
  17.         String ISOStr = Utctime2Datetime.getISO8601TimestampFromDateStr(dateTime);
  18.         System.out.println("ISOStr = " + ISOStr);
  19.  
  20.  
  21.     }
  22.  
  23.  
  24.     public static String getDateStrFromISO8601Timestamp(String ISOdate){
  25.         DateTimeFormatter dtf1 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
  26.         DateTime dt= dtf1.parseDateTime(ISOdate);
  27.         DateTimeFormatter dtf2= DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
  28.  
  29.         return dt.toString(dtf2);
  30.     }
  31.  
  32.     public static String getISO8601TimestampFromDateStr(String timestamp){
  33.         java.time.format.DateTimeFormatter dtf1 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  34.         LocalDateTime ldt = LocalDateTime.parse(timestamp,dtf1);
  35.         ZoneOffset offset = ZoneOffset.of("+08:00");
  36.         OffsetDateTime date = OffsetDateTime.of(ldt ,offset);
  37.         java.time.format.DateTimeFormatter dtf2 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
  38.         return date.format(dtf2 );
  39.  
  40.     }
  41.  
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement