Java LocalDateTime Calculate parseTimeStamp(LocalDateTime actualDate, LocalDateTime checkedDate, boolean isDateFrom)

Here you can find the source of parseTimeStamp(LocalDateTime actualDate, LocalDateTime checkedDate, boolean isDateFrom)

Description

parse Time Stamp

License

Open Source License

Declaration

public static LocalDateTime parseTimeStamp(LocalDateTime actualDate, LocalDateTime checkedDate,
            boolean isDateFrom) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.time.LocalDateTime;

public class Main {
    public static LocalDateTime parseTimeStamp(LocalDateTime actualDate, LocalDateTime checkedDate,
            boolean isDateFrom) {
        // Check for date
        if (checkedDate != null && actualDate != null && checkIfDateExist(checkedDate)
                && !checkIfDateExist(actualDate)) {
            if (!isDateFrom) {
                actualDate = checkedDate.toLocalDate().atTime(actualDate.getHour(), actualDate.getMinute());
            }/*  ww w.j  ava  2 s  .  c  om*/
        }
        // Check for time
        if (checkedDate != null && actualDate != null && checkIfTimeExist(checkedDate)
                && !checkIfTimeExist(actualDate)) {
            actualDate = actualDate.toLocalDate().atTime(checkedDate.getHour(), checkedDate.getMinute());
        }
        if (actualDate != null && !checkIfTimeExist(actualDate)) {
            if (isDateFrom) {
                actualDate = floorDate(actualDate);
            } else {
                actualDate = ceilDate(actualDate);
            }
        }
        return actualDate;
    }

    public static boolean checkIfDateExist(LocalDateTime date) {
        LocalDateTime currentDate = LocalDateTime.now();
        return currentDate.getDayOfYear() != date.getDayOfYear() || currentDate.getMonth() != date.getMonth()
                || currentDate.getYear() != date.getYear();
    }

    public static boolean checkIfTimeExist(LocalDateTime date) {
        LocalDateTime currentTime = LocalDateTime.now();
        return currentTime.getHour() != date.getHour() || currentTime.getMinute() != date.getMinute();
    }

    /**
     * @@author A0139812A
     * Performs a "floor" operation on a LocalDateTime, and returns a new LocalDateTime
     * with time set to 00:00.
     * 
     * @param dateTime   LocalDateTime for operation to be performed on.
     * @return           "Floored" LocalDateTime.
     */
    public static LocalDateTime floorDate(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }

        return dateTime.toLocalDate().atTime(0, 0);
    }

    /**
     * Performs a "ceiling" operation on a LocalDateTime, and returns a new LocalDateTime
     * with time set to 23:59.
     * 
     * @param dateTime   LocalDateTime for operation to be performed on.
     * @return           "Ceiled" LocalDateTime.
     */
    public static LocalDateTime ceilDate(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }

        return dateTime.toLocalDate().atTime(23, 59);
    }
}

Related

  1. last(LocalDateTime from, int dayOfWeek)
  2. localDateTime2LocalDate(LocalDateTime localDateTime)
  3. ms2LocalDateTime(final long ms)
  4. oldDateToISO8601LocalDateTime(Date nextColumnDate)
  5. parseStringToLocalDateTimeWithSpecifiedTime(String strDate, String time)
  6. printDateTime(LocalDateTime dateTime)
  7. relativeDateTime(final LocalDateTime contextDate, final String str, final boolean add)
  8. safeCreateFromValue(LocalDateTime datetime, int year, int month, int day, int hour, int minute, int second)
  9. secondsBetween(LocalDateTime date1, LocalDateTime date2)