Playing with Java Dates


The below class gives different dates with start and end times:

package com.test.utils;

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

/**
*
* This class is used to get the Dates of particular period
*
*/
public class DateUtilities {

/**
* This variable is used to DateFormat without time stamp
*/
public static final String dateFormat = “dd-MMM-yy”;
/**
*This variable is used to DateFormat with time stamp
*/
public static final String dateFormatWithTimestamp = “dd-MMM-yy hh:mm:ss a”;
public static final String pmdateString = ” 11:59:59 PM”;
public static final String amdateString = ” 12:00:00 AM”;

/**
* @param date
*             Date
* @return formatedDate
*             String
*/
private static String getForamtedDate(Date date){
SimpleDateFormat simpledateFormat = new SimpleDateFormat(dateFormat);
String formatedDate = simpledateFormat.format(date);
return formatedDate;
}
/**
* @param date
*             Date
* @return formatedDate
*             String
*/
private static String getForamtedDateWithTimestamp(Date date){
SimpleDateFormat simpledateFormat = new SimpleDateFormat(dateFormatWithTimestamp);
String formatedDate = simpledateFormat.format(date);
return formatedDate;
}
/**
*  returns the last 12 hours start and end time
* @return stDate+”,”+edDate
*             String
*/
private static String getLast12Hours(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis()-12*60*60*1000);
String stDate = getForamtedDateWithTimestamp(cal.getTime());
String edDate = getForamtedDateWithTimestamp(new Date());
return stDate+”,”+edDate;
}
/**
* returns the last 24 hours start and end time
* @return stDate+”,”+edDate
*             String
*/
private static String getLast24Hours(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis()-24*60*60*1000);
String stDate = getForamtedDateWithTimestamp(cal.getTime());
String edDate = getForamtedDateWithTimestamp(new Date());
return stDate+”,”+edDate;
}
/**
* returns the today start and current time
* @return stDate+”,”+edDate
*             String
*/
private static String getToday(){
String stDate = getForamtedDate(new Date());
String edDate = getForamtedDateWithTimestamp(new Date());
stDate = stDate+amdateString;
return stDate+”,”+edDate;
}
/**
* returns the yesterday start and end time
* @return stDate+”,”+edDate
*             String
*/
private static String getYesterDay(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String stDate = getForamtedDate(cal.getTime());
String edDate = getForamtedDate(cal.getTime());
stDate = stDate+amdateString;
edDate = edDate+pmdateString;
return stDate+”,”+edDate;
}
/**
* returns the thisweek start time and current time
* @return stDate+”,”+edDate
*             String
*/
private static String getThisWeekDates(){
Calendar weekStartDay = Calendar.getInstance();
int val = weekStartDay.get(Calendar.DAY_OF_WEEK);
weekStartDay.add(Calendar.DATE,-(val-1));
String stDate = getForamtedDate(weekStartDay.getTime());
String edDate = getForamtedDateWithTimestamp(new Date());
stDate = stDate+amdateString;
return stDate+”,”+edDate;
}
/**
* returns the last week start time and end time
* @return stDate+”,”+edDate
*             String
*/
private static String getLastWeekDates(){
Calendar weekStartDay = Calendar.getInstance();
Calendar weekEndDay = Calendar.getInstance();
int val = weekStartDay.get(Calendar.DAY_OF_WEEK);
weekStartDay.add(Calendar.DATE,-(val+6));
String stDate =  getForamtedDate(weekStartDay.getTime());
weekEndDay.add(Calendar.DATE,-(val));
String edDate = getForamtedDate(weekEndDay.getTime());
stDate = stDate+amdateString;
edDate = edDate+pmdateString;
return stDate+”,”+edDate;
}
/**
* returns the last 7 days start time and end time
* @return stDate+”,”+edDate
*             String
*/
private static String getLast7Days(){
Calendar today = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();
today.add(Calendar.DATE, -7);
endDate.add(Calendar.DATE, -1);
String startDay = getForamtedDate(today.getTime());
String thisDay = getForamtedDate(endDate.getTime());
thisDay = thisDay + pmdateString;
startDay = startDay+amdateString;
return startDay+”,”+thisDay;

}
/**
* returns the last 30 days start time and end time
* @return stDate+”,”+edDate
*             String
*/
private static String getLast30Days(){
Calendar today = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();
today.add(Calendar.DATE, -30);
String startDay = getForamtedDate(today.getTime());
endDate.add(Calendar.DATE, -1);
String thisDay = getForamtedDate(endDate.getTime());
startDay = startDay+amdateString;
thisDay = thisDay +pmdateString;
return startDay+”,”+thisDay;
}
/**
* returns the current month start time and end time
* @return startDateOfMonth+”,”+todayUptoThisTime
*                 String
*/
public static String getThisMonthDates(){
Calendar today = Calendar.getInstance();
today.set(Calendar.DATE, today.getMinimum(Calendar.DATE));
String startDateOfMonth = getForamtedDate(today.getTime());
String todayUptoThisTime = getForamtedDateWithTimestamp(new Date());
startDateOfMonth = startDateOfMonth+amdateString;
return startDateOfMonth+”,”+todayUptoThisTime;
}
/**
* returns the last Month start time and end time
* @return startDate+”,”+endDate
*                 String
*/
public static String getLastMonthDates(){
Calendar lastMonthStartDate = Calendar.getInstance();
Calendar lastMonthEndDate = Calendar.getInstance();
lastMonthStartDate.add(Calendar.MONTH,-1);
lastMonthStartDate.set(Calendar.DATE,1);
lastMonthEndDate.set(Calendar.DATE, 0);
String startDate = getForamtedDate(lastMonthStartDate.getTime());
String endDate = getForamtedDate(lastMonthEndDate.getTime());
startDate = startDate+amdateString;
endDate = endDate + pmdateString;
return startDate+”,”+endDate;
}

public static void main(String a[]){

DateUtilities dt = new DateUtilities();

System.out.println(“Last 12 Hours : “+getLast12Hours());
System.out.println(“Last 24 Hours: “+getLast24Hours());
System.out.println(“To day : “+getToday());
System.out.println(“Yester Day : “+getYesterDay());
System.out.println(“This week : “+getThisWeekDates());
System.out.println(“Last Week : “+getLastWeekDates());
System.out.println(“Last 7 Days : “+getLast7Days());
System.out.println(“This Month : “+getThisMonthDates());
System.out.println(“Last Month : “+getLastMonthDates());
System.out.println(“Last 30 Days : “+getLast30Days());

}

}

Below class explains how to convert String to Date

package com.test.utils;

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

public class StringToDateConverter
{
public static void main(String[] args)
{
DateFormat df = new SimpleDateFormat(“dd-MMM-yy hh”);

try
{
Date today = df.parse(“20-Jun-05 22:22:10 PM”);
System.out.println(“Today = ” + df.format(today));
} catch (ParseException e)
{
e.printStackTrace();
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
May 2018
M T W T F S S
« Mar    
 123456
78910111213
14151617181920
21222324252627
28293031  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 258,490 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100