Code Robo
Formatter
Comparator
Tester
Merger
Converter
Utility
Java Code Complience
Validator
EncoderDecoder
Virtual Service
Java Date Utility Functions
       Talk to EasyAssistant

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.time.Instant;
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 long getTimeDffBetwn2DatesInMiliSec(Date higherDate, Date lowerDate) {
		return ((higherDate.getTime() - lowerDate.getTime()));
	}

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

	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 boolean isExpiredOnADate(Date startDate, Date endDate, int diffInMinute) throws Exception {
		// Date date = new Date();
		System.out.println("startDate" + startDate);

		long timeDiff = getTimeDffBetwn2DatesInSec(endDate, startDate);

		if (timeDiff > diffInMinute * 60)
			return true;
		else
			return false;

	}

	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);
	}

	public static String getCurrentDateInUTC(int day) {

		return Instant.now().toString();

	}


	public static Date getDateObjectWithTimeZeroZero(String dateStr) {

		final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dateObj = new Date();

		try {
			dateObj = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return dateObj;
	}

	public static String getDateObjectWithTimeZeroZero(Date date) {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = "";
		dateStr = formatter.format(date);
		return dateStr;
	}

	public static long dayDiffBetweenTwoDates(Date higherDate, Date lowerDate) throws ParseException {

		final SimpleDateFormat formatterHigher = new SimpleDateFormat("yyyy-MM-dd");
		final String higherDateStr = formatterHigher.format(higherDate);
		final SimpleDateFormat sdfHigher = new SimpleDateFormat("yyyy-MM-dd");
		final java.util.Date higherDateWithZeroTime = sdfHigher.parse(higherDateStr);

		final SimpleDateFormat formatterLower = new SimpleDateFormat("yyyy-MM-dd");
		final String lowerDateStr = formatterLower.format(lowerDate);
		final SimpleDateFormat sdfLower = new SimpleDateFormat("yyyy-MM-dd");
		final java.util.Date lowerDateWithZeroTime = sdfLower.parse(lowerDateStr);

		long dayDiff = ((higherDateWithZeroTime.getTime() - lowerDateWithZeroTime.getTime()) / (1000 * 60 * 60 * 24));
		return dayDiff;
	}

}





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



Post Your Comment:
Name :
Email ( Optional) :
Comments / Suggestion (* Required) It is required: :
: