Here you can find the source of safeCreateFromValue(LocalDateTime datetime, int year, int month, int day, int hour, int minute, int second)
public static LocalDateTime safeCreateFromValue(LocalDateTime datetime, int year, int month, int day, int hour, int minute, int second)
//package com.java2s; //License from project: Open Source License import java.time.LocalDateTime; public class Main { public static LocalDateTime safeCreateFromValue(LocalDateTime datetime, int year, int month, int day, int hour, int minute, int second) { if (isValidDate(year, month, day) && isValidTime(hour, minute, second)) { datetime = safeCreateFromValue(datetime, year, month, day); datetime = datetime.plusHours(hour - datetime.getHour()); datetime = datetime.plusMinutes((minute - datetime.getMinute())); datetime = datetime.plusSeconds(second - datetime.getSecond()); }// w w w . j a va 2 s .co m return datetime; } public static LocalDateTime safeCreateFromValue(LocalDateTime datetime, int year, int month, int day) { if (isValidDate(year, month, day)) { datetime = datetime.plusYears(year - datetime.getYear()); datetime = datetime.plusMonths((month - datetime.getMonthValue())); datetime = datetime.plusDays(day - datetime.getDayOfMonth()); } return datetime; } public static Boolean isValidDate(int year, int month, int day) { if (year < 1 || year > 9999) { return false; } Integer[] validDays = { 31, year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; return month >= 1 && month <= 12 && day >= 1 && day <= validDays[month - 1]; } public static boolean isValidTime(int hour, int minute, int second) { return 0 <= hour && hour <= 23 && 0 <= minute && minute <= 59 && 0 <= second && second <= 59; } }