
This Java Programming tutorial contains various real-time project-related examples where utilities function written for Date. Please find the programs below and let us know if you want to add any specific one –
Convert ‘yyyy-mm-dd’ Date to java.sql.Date
This Java Programming helps to convert Date String in ‘yyyy-mm-dd’ format to java SQL Date format. This program will be used when the date came from UI component and need to persist in the database. Please find the program below –
package com.kw.datetime;
import java.sql.Date;
import java.time.LocalDate;
/**
* This Java class helps to write programming examples related to Java Date API.
* @author dknitk
*
*/
public class JavaDateAPIConvrtStringToSQLDate {
/**
* This main method helps to execute the Java Program.
* We can pass various Input to test the functionality.
* @param args
*/
public static void main(String[] args) {
System.out.println("Welcome to Java Date API Program Example!");
JavaDateAPIConvrtStringToSQLDate apiExamples = new JavaDateAPIConvrtStringToSQLDate();
System.out.println("**** Converted Date Is :::" + apiExamples.convertStringToSQLDate("2021-05-22"));
}
/**
* This program helps to convert String ('yyyy-mm-dd') Date to Java SQL Date.
* @param dateAsString
* @return java.sql.Date
* @since KW-12345
*/
public java.sql.Date convertStringToSQLDate(String dateAsString) {
if (!isNullOrEmpty(dateAsString)) {
LocalDate localDate = LocalDate.parse(dateAsString);
return Date.valueOf(localDate);
}
return null;
}
/**
* This method helps to verify String as null and empty.
* @param data
* @return boolean
*/
private boolean isNullOrEmpty(String data) {
return data == null || "".equals(data.trim());
}
}
Program Output:
Welcome to Java Date API Program Example!
**** Converted Date Is :::2021-05-22
Difference Between Two Date to get Year, Month and Days
This Java Programming helps to get a number of days, years, and months between two given dates. Please find the program below –
package com.kw.datetime;
import java.sql.Date;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
/**
* This Java class helps to write programming get two date difference. It will
* provide to get Months, Days and Year.
*
* @author dknitk
*
*/
public class JavaDateAPITwoDateDifference {
/**
* This main method helps to execute the Java Program. We can pass various Input
* to test the functionality.
*
* @param args
*/
public static void main(String[] args) {
System.out.println("Welcome to Java Date API Program Example!");
JavaDateAPITwoDateDifference apiExamples = new JavaDateAPITwoDateDifference();
LocalDate from = LocalDate.of(2021, 7, 30);
LocalDate to = LocalDate.of(2022, 8, 30);
System.out.println("**** Program Execution 1****");
System.out.println("**** Total Number of Year between Dates Is :::" + apiExamples.getYears(from, to));
System.out.println("**** Total Number of Month between Dates Is :::" + apiExamples.getMonths(from, to));
System.out.println("**** Total Number of Day between Dates Is :::" + apiExamples.getDays(from, to));
from = LocalDate.of(1990, 7, 30);
to = LocalDate.of(2022, 11, 30);
System.out.println("**** Program Execution 2****");
System.out.println("**** Total Number of Year between Dates Is :::" + apiExamples.getYears(from, to));
System.out.println("**** Total Number of Month between Dates Is :::" + apiExamples.getMonths(from, to));
System.out.println("**** Total Number of Day between Dates Is :::" + apiExamples.getDays(from, to));
}
/**
* This program helps to get the total number of Months from two date
* difference.
*
* @param from
* @param to
* @return Long
* @since KW-12345
*/
public Long getMonths(LocalDate from, LocalDate to) {
if (!Objects.isNull(from) && !Objects.isNull(to)) {
return ChronoUnit.MONTHS.between(from, to);
}
return null;
}
/**
* This program helps to get the total number of Days from two date difference.
*
* @param from
* @param to
* @return Long
* @since KW-12345
*/
public Long getDays(LocalDate from, LocalDate to) {
if (!Objects.isNull(from) && !Objects.isNull(to)) {
return ChronoUnit.DAYS.between(from, to);
}
return null;
}
/**
* This program helps to get the total number of Year from two date difference.
*
* @param from
* @param to
* @return Long
* @since KW-12345
*/
public Long getYears(LocalDate from, LocalDate to) {
if (!Objects.isNull(from) && !Objects.isNull(to)) {
return ChronoUnit.YEARS.between(from, to);
}
return null;
}
}
Program Output:
Welcome to Java Date API Program Example!
* Program Execution 1*
**** Total Number of Year between Dates Is :::1
**** Total Number of Month between Dates Is :::13
**** Total Number of Day between Dates Is :::396
**** Program Execution 2****
**** Total Number of Year between Dates Is :::32
**** Total Number of Month between Dates Is :::388
**** Total Number of Day between Dates Is :::11811