package com.clean.a; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class DateTest{ public static void main(String[] args) throws ParseException { //Date d = new Date(); String x= "2018-02-04 15:30:40"; SimpleDateFormat dfx = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date m = dfx.parse(x); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); df.setTimeZone(TimeZone.getTimeZone("UTC")); String res = df.format(m); String k = res + "Z"; System.out.println("res = " + k); } }
package com.clean; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyTest { public static void main(String[] args) { String x= "2018-04-10T10:15:58.858Z"; String y = UTCStringtODefaultString(x); System.out.println("y = " + y); } public static String UTCStringtODefaultString(String UTCString) { try { UTCString = UTCString.replace("Z", " UTC"); SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z"); SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = utcFormat.parse(UTCString); return defaultFormat.format(date); } catch(ParseException pe) { pe.printStackTrace(); return null; } } }