Java OffsetDateTime compareChrono(TemporalAccessor date1, TemporalAccessor date2)

Here you can find the source of compareChrono(TemporalAccessor date1, TemporalAccessor date2)

Description

compare Chrono

License

MIT License

Declaration

public static int compareChrono(TemporalAccessor date1, TemporalAccessor date2) 

Method Source Code

//package com.java2s;
/*!/*from  w w  w .jav  a  2s  .  com*/
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.UnsupportedTemporalTypeException;

public class Main {
    public static int compareChrono(TemporalAccessor date1, TemporalAccessor date2) {
        return compareChrono(date1, date2, false);
    }

    public static int compareChrono(TemporalAccessor date1, TemporalAccessor date2, boolean bothSupportOnly) {
        return compareChrono(date1, date2, bothSupportOnly, false, false);
    }

    public static int compareChrono(TemporalAccessor date1, TemporalAccessor date2, boolean bothSupportOnly,
            boolean nullGreater, boolean unsupportedGreater) {
        if (date1 == null) {
            if (date2 == null) {
                return 0;
            } else {
                return (nullGreater) ? 1 : -1;
            }
        } else if (date2 == null) {
            return (nullGreater) ? -1 : 1;
        }

        boolean matched = false;
        int cmp;
        int cmpUnsupport = 0;

        for (TemporalField field : new TemporalField[] { ChronoField.YEAR, ChronoField.MONTH_OF_YEAR,
                ChronoField.DAY_OF_MONTH, ChronoField.DAY_OF_WEEK, ChronoField.HOUR_OF_DAY,
                ChronoField.MINUTE_OF_HOUR, ChronoField.SECOND_OF_MINUTE, ChronoField.MILLI_OF_SECOND,
                ChronoField.NANO_OF_SECOND }) {
            try {
                cmp = compareTemporalField(date1, date2, field, unsupportedGreater);
                if (cmp == -2 || cmp == 2) {
                    if (bothSupportOnly) {
                        if (cmpUnsupport == 0) {
                            cmpUnsupport = cmp;
                        }
                    } else {
                        return cmp;
                    }
                } else if (cmp != 0) {
                    return cmp;
                } else {
                    matched = true;
                }
            } catch (UnsupportedTemporalTypeException e) {
                // NOP
            }
        }

        if (hasZone(date1) && hasZone(date2)) {
            try {
                cmp = compareTemporalField(date1, date2, ChronoField.OFFSET_SECONDS, unsupportedGreater);
                if (cmp == -2 || cmp == 2) {
                    if (bothSupportOnly) {
                        if (cmpUnsupport == 0) {
                            cmpUnsupport = cmp;
                        }
                    } else {
                        return cmp;
                    }
                } else if (cmp != 0) {
                    return cmp;
                } else {
                    matched = true;
                }
            } catch (UnsupportedTemporalTypeException e) {
                // NOP
            }
        }

        if (matched) {
            return 0;
        } else {
            return cmpUnsupport;
        }
    }

    private static int compareTemporalField(TemporalAccessor date1, TemporalAccessor date2, TemporalField field,
            boolean unsupportedGreater) {
        if (date1.isSupported(field)) {
            if (date2.isSupported(field)) {
                int cmp = date1.get(field) - date2.get(field);
                if (cmp == 0) {
                    return 0;
                } else if (cmp < 0) {
                    return -1;
                } else {
                    return 1;
                }
            } else {
                return (unsupportedGreater) ? -2 : 2;
            }
        } else {
            if (date2.isSupported(field)) {
                return (unsupportedGreater) ? 2 : -2;
            } else {
                throw new UnsupportedTemporalTypeException(String.valueOf(field));
            }
        }
    }

    public static boolean hasZone(TemporalAccessor date) {
        if (date == null) {
            return false;
        }

        if (date instanceof ZonedDateTime) {
            return true;
        } else if (date instanceof OffsetDateTime) {
            return true;
        } else if (date instanceof OffsetTime) {
            return true;
        }

        return false;
    }
}

Related

  1. asDate(Calendar calendar)
  2. cloneInternally(E source, Map visited)
  3. compareOffsetDateTimes(OffsetDateTime offsetDateTime1, OffsetDateTime offsetDateTime2)
  4. convertDateToOffsetDateTime(Date date)
  5. getCurrentISODate()
  6. getDateTimeString(OffsetDateTime time)