1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.serializer.SerializerFeature;
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone;
public class A { public static void main(String[] args) throws Exception { new A().test(); }
public static String toISO8601UTC(Date date) { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(tz); return df.format(date); }
public static Date fromISO8601UTC(String dateStr) { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(tz);
try { return df.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); }
return null; }
public void test() { String input = "2020-11-05T16:00:00.000Z"; Date date = fromISO8601UTC(input); System.out.println(date); DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(df1.format(date));
Model model = new Model(); model.startTime = date; String data = com.alibaba.fastjson.JSON.toJSONStringWithDateFormat(model, "yyyy-MM-dd", SerializerFeature.DisableCircularReferenceDetect); System.out.println(data); }
public static class Model { @JSONField(format = "yyyy-MM-dd hh:mm") public Date startTime;
@JSONField(format = "yyyy-MM-dd") public Date endTime;
@Override public String toString() { return "Model{" + "startTime=" + startTime + ", endTime=" + endTime + '}'; } } }
|