Tagged: date

format/parse ISO 8601 ignoring the fraction


    static Instant parseIsoInstant(final String formatted) {
        return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(formatted))
                .with(ChronoField.NANO_OF_SECOND, 0L);
    }

    static String formatIsoInstant(final TemporalAccessor parsed) {
        return DateTimeFormatter.ISO_INSTANT.format(
                Instant.from(parsed).with(ChronoField.NANO_OF_SECOND, 0L));
    }

    static Date parseIsoDate(final String formatted) {
        return Date.from(parseIsoInstant(formatted));
    }

    static String formatIsoDate(final Date parsed) {
        return formatIsoInstant(parsed.toInstant());
    }

ISO 8601 Date/Time


RFC-3339 / 5.6. Internet Date/Time Format에 ISO-8601 Date/Time 에서 대해 설명하고 있다.
년년년년-월월-일일T시시:분분:초초 까지는 대충 쓰면 되는데 그 뒤의 부분들이 문제다. (초초 부분도 0~59 만 사용되는 것이 아니지만 일단 넘어가자.)

time-secfrac

time-secfrac    = "." 1*DIGIT

초초 뒤에 . 을 붙이고 1개 이상의 초 단위 이하의 값을 사용할 수가 있다.

1976-10-31T10:00:00.2134213213213213213213Z

“Z” / time-numoffset

보통의 경우에 다음과 같이 오프셋을 사용할 수 있다.

1976-10-31T10:00:00+09:00
1976-10-31T10:00:00-08:00

근데 00:00 앞에는 + 를 붙여야 하는가 - 를 붙여야 하는가?
머리를 어떻게 굴렸는지는 잘 모르겠지만 Z 를 사용하기로 했나보다.

1976-10-31T10:00:00Z

4.3. Unknown Local Offset Convention 에 보면 Z+00:00 과 같고 -00:00 은 TimeZone 을 모를(unknown) 때 사용한다고 한다.

mapping a milliseconds parameter directly into a date instance in jax-rs


@POST
public void myResource(
    @QueryParam("milliseconds")
    @NotNull
    final Long milliseconds) {

    final Date date = new Date(milliseconds);
    myService(date);
}

I wrote some codes using ParamConverter and ParamConverterProvider.
And now I can do like this.

@POST
public void myResource(
    @QueryParam("milliseconds")
    @NotNull
    @MillisecondsParam
    final Date date) {

    myService(date);
}

Here are 3 classes for this.

changing a nullable milliseconds to a nullable date using optional.


public Date toDate(@Nullable Long millis) {
    //return millis == null ? null : new Date(millis);
    return Optional.ofNullable(millis).map(Date::new).orElse(null);
}

Returning an Optional rather than a nullable Date instance is a better idea.

public Optional<Date> toDate(@Nullable Long millis) {
    return Optional.ofNullable(millis).map(Date::new);
}

adb shell date now


I don’t like iOS but I hate Android.

References

$

... $ adb shell date -s `date +"%Y%m%d.%H%M%S"`

>

PS ...> adb shell date -s $(get-date -format yyyyMMdd.HHmmss)

public class AdbShellDateNow {

    public static void main(final String[] args)
        throws java.io.IOException, InterruptedException {

        final long now = System.currentTimeMillis() / 1000L;
        final ProcessBuilder builder =
            new ProcessBuilder("adb", "shell", "date", Long.toString(now));
        builder.redirectErrorStream(true);
        builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

        final Process process = builder.start();
        process.waitFor();
    }
}