Code Robo
Virtual Service
Tester / Comparator
Converter
Utility
Java Code Complience
Validator
Formatter
EncoderDecoder

Java Date Utility Functions

Java Date Utility Functions. It provides sample java date functions. Copuple of examples are shown for Java date function which can be copied from here and used in any java project. It uses java.util.Date class . Sample code uses java.text.SimpleDateFormat class to format the date.


Java Date Util File Content:
package sg.ardpfnd.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateUtil {

	private static Logger logger = Logger.getLogger(DateUtil.class.getName());

	public static void main(String args[]) {
		Date date = new Date();

		System.out.println("date" + date);
		System.out.println("date--" + getDateAsStringInYYYYMMDD(date));

		DateFormat sdf = SimpleDateFormat.getInstance();
		System.out.println("" + getTimeNowDateInAFormat("yyyy-MM-dd HH:mm:ss.S"));
		String str = getTimeNowDateInAFormat("yyyy-MM-dd HH:mm:ss.S");
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		Date date2;
		try {
			date2 = dateFormatter.parse("" + str);
			System.out.println("Format 12:   " + date2);
		} catch (ParseException e) {

			e.printStackTrace();
		}

		// Shows "Mon, 2012-10-8 at 8:17:6 AM EDT"

	}

	public static String getDateInyyyy_MM_ddHHmmss_S(Date date) {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		return formatter.format(date);

	}

	public static Date getDateFromStringInyyyy_MM_ddHHmmss_S(String dateStr) throws Exception {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		return formatter.parse(dateStr);

	}

	public static String getDateAsStringInYYYYMMDD(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date);

	}

	// formatStr = yyyy-MM-dd HH:mm:ss.S
	public static String getTimeNowDateInAFormat(String formatStr) {

		Date date = new Date();
		System.out.println("date" + date);
		SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
		return formatter.format(date);

	}

	// formatStr = yyyy-MM-dd HH:mm:ss.S
	public static String getTimeNowDateInyyyy_MM_ddHHmmss_S() {

		Date date = new Date();
		System.out.println("date" + date);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
		return formatter.format(date);

	}

	public static String getDateOfToday(String formatStr) {
		return getTimeNowDateInAFormat(formatStr);
	}

	public static String getToday(String formatStr) {
		return getTimeNowDateInAFormat(formatStr);
	}

	public static String getTodayinYYYYMMDD() {

		Date date = new Date();
		System.out.println("date" + date);
		return getDateAsStringInYYYYMMDD(date);
	}

	public static Date getDateFromStringInYYYYMMDD(String dateStr) throws Exception {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.parse(dateStr);

	}

	public static String getCurrentDateInYYYYMMDD() throws Exception {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		System.out.println("date" + date);
		return getDateAsStringInYYYYMMDD(date);

	}

	public static String getCurrentDateInYYYYMMDD(String dateStr) throws Exception {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		System.out.println("date" + date);
		return getDateAsStringInYYYYMMDD(date);

	}

	public static int daysBetweenTwoDates(Date higherDate, Date lowerDate) {
		return (int) ((higherDate.getTime() - lowerDate.getTime()) / (1000 * 60 * 60 * 24));
	}

	public static long getTimeDffBetwn2Dates(Date higherDate, Date lowerDate) {
		return ((higherDate.getTime() - lowerDate.getTime()));
	}

	public static boolean isExpiredAsOfToday(Date dateToComp) {
		Date date = new Date();
		System.out.println("date" + date);
		return date.before(dateToComp);

	}

	public static boolean isExpiredAsOfToday(String dateToComp) throws Exception {
		Date date = new Date();
		logger.info("ExpierDateOfUser=" + dateToComp);
		boolean isExp = date.after(getDateFromStringInYYYYMMDD(dateToComp));
		logger.info("Expiered=" + isExp);
		return isExp;
	}

	public static String getDaysAfter90Days() {
		return getDaysAfter_N_Days(90);
	}

	public static String getDaysAfter_N_Days(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, day);
		Date d = c.getTime();
		System.out.println("30 days after today is: " + d);
		return getDateAsStringInYYYYMMDD(d);

	}

	public static String getDaysAfter_N_DaysStr(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, day);
		Date d = c.getTime();
		System.out.println("30 days after today is: " + d);

		return getDateAsStringInYYYYMMDD(d);
	}

}





Please provide your feedback here
It has been explained in this video (https://youtu.be/bxkNb0GuELY). Please watch it.