Java SQL Date serialize(Instant sourceValue)

Here you can find the source of serialize(Instant sourceValue)

Description

serialize

License

Apache License

Declaration

public static String serialize(Instant sourceValue) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Main {
    private static final DateTimeFormatter ISO_DATE_FORMAT_MILLIS = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX").withZone(ZoneId.of("UTC"));
    private static final DateTimeFormatter ISO_DATE_FORMAT_SECONDS = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ssX").withZone(ZoneId.of("UTC"));
    private static final DateTimeFormatter ISO_LOCAL_TIME_FORMAT = DateTimeFormatter.ISO_LOCAL_TIME;
    private static final DateTimeFormatter ISO_LOCAL_FORMAT = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    private static final DateTimeFormatter ISO_ZONED_FORMAT = DateTimeFormatter.ISO_ZONED_DATE_TIME;

    public static String serialize(Instant sourceValue) {
        if (sourceValue.getNano() == 0) {
            return ISO_DATE_FORMAT_SECONDS.format(sourceValue);
        }//from w  ww  . jav a 2  s.com
        return ISO_DATE_FORMAT_MILLIS.format(sourceValue);
    }

    public static String serialize(LocalDateTime date) {
        return date.format(ISO_LOCAL_FORMAT);
    }

    public static String serialize(ZonedDateTime date) {
        return date.format(ISO_ZONED_FORMAT);
    }

    public static String serialize(LocalTime date) {
        return date.format(ISO_LOCAL_TIME_FORMAT);
    }

    public static String serialize(Date date) {
        return serialize(fromUtilDate(date));
    }

    public static LocalDateTime fromUtilDate(Date date) {
        if (date.getClass() == Date.class) {
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }
        return fromUtilDate(new Date(date.getTime())); //hack for old java.sql.Date
    }
}

Related

  1. isSimpleType(Object obj)
  2. issue20()
  3. now()
  4. nowPlus(int parts, int value)
  5. parseStringValue(Class returnType, String value)
  6. stringToMillis(String str)
  7. toAge(String birthDay)
  8. today()
  9. today()