Monday, November 10, 2014

Java - Date handling examples and code.

A simple class to deal with most date functions. The methods give a general idea about using dates in Java. I guess this is would be useful to beginners trying to deal with the Date object.

Feel free to use this code anywhere required.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author String
 */
public class DateUtility {

    private static SimpleDateFormat mmddyyyyDateFormat = new SimpleDateFormat("MM-dd-yyyy");
    private static SimpleDateFormat yyyymmddDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static Locale locale = Locale.ENGLISH;

    public static String getCurrentDay() {
        Calendar cal = Calendar.getInstance();
        return yyyymmddDateFormat.format(cal.getTime()).toString();
    }

    public static Date getCurrentDate() {
        return new Date(Calendar.getInstance().getTimeInMillis());
    }

    public static java.sql.Date getCurrentSQLDate() {
        return new java.sql.Date(Calendar.getInstance().getTimeInMillis());
    }

    public static java.sql.Timestamp getCurrentTimestamp() {
        return new java.sql.Timestamp(Calendar.getInstance().getTimeInMillis());
    }

    public static int getCurrentHour() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    public static int getCurrentWeek() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    public static int getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH);
    }

    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR);
    }

    public static int getDaysInCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static int getDaysInCurrentYear() {
        Calendar cal = Calendar.getInstance();
        return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    public static java.sql.Date getFirstDayOfCurrentWeek() {
        // Get calendar set to current date and time
        Calendar cal = Calendar.getInstance();
        // Set the calendar to monday of the current week
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_WEEK));
        java.sql.Date dt = new java.sql.Date(cal.getTimeInMillis());
        return dt;
    }

    public static java.sql.Date getFirstDayOfMonth() {
        // Get calendar set to current date and time
        Calendar cal = Calendar.getInstance();
        // Set the calendar to first day of the current month
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_MONTH));
        java.sql.Date dt = new java.sql.Date(cal.getTimeInMillis());
        return dt;
    }

    public static long diffDays(Date stDate, Date endDate) {
        long milsecs1 = stDate.getTime();
        long milsecs2 = endDate.getTime();
        long diff = milsecs2 - milsecs1;
        long ddays = diff / (24 * 60 * 60 * 1000);
        return ddays;
    }

    public static long diffHours(Date stDate, Date endDate) {
        long milsecs1 = stDate.getTime();
        long milsecs2 = endDate.getTime();
        long diff = milsecs2 - milsecs1;
        long dhours = diff / (60 * 60 * 1000);
        return dhours;
    }

    public static Date getCalendarDate(String stringDate) {
        Calendar calendar = Calendar.getInstance();
        String sDate[] = stringDate.split("-");
        calendar.set(Integer.parseInt(sDate[0]), Integer.parseInt(sDate[1]), Integer.parseInt(sDate[2]));
        return calendar.getTime();
    }

    public static Date getDateAfterDurationInDays(int duration) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, duration);
        return calendar.getTime();
    }
}

No comments:

Post a Comment