Thursday, 10 January 2013

Date Manipulations using Calendar class in Java

Date Manipulations using Calendar class in Java



Date using Calender class in Java

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

In this post I'm going to explain you how to get the first day of current week, last day of current week, first day of previous week, last day of previous week, and similarly for month and year.

Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time. Using this Instance we are going to set  the calender fields accordingly to get the specific day of the year.

 java.util.Calendar cal = java.util.Calendar.getInstance(); //Calendar instance set to current Date and Time

Let's take a simple date formatter to get the date in our desired format.

 DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy"); // Give date in dd-mm-yyyy format.
  
First & Last Date of Current Week
cal.set(java.util.Calendar.DAY_OF_WEEK,1);  // will reset to sunday, first day of week
String currentWeekFirstDay=dateFormat.format(cal.getTime());
              
cal.set(java.util.Calendar.DAY_OF_WEEK,7); // will reset to saturday, last day of week
String currentWeekLastDay=dateFormat.format(cal.getTime());

              
First & Last Date of Previous Week

cal.add(java.util.Calendar.WEEK_OF_YEAR,-1); // Will reduce one week time
              
cal.set(java.util.Calendar.DAY_OF_WEEK,1);  // will reset to sunday, first day of week
String previousWeekFirstDay=dateFormat.format(cal.getTime());
              
cal.set(java.util.Calendar.DAY_OF_WEEK,7);  // will reset to saturday, last day of week
String previousWeekLastDay=dateFormat.format(cal.getTime());

              
First & last Date of current Month
cal.set(java.util.Calendar.DAY_OF_MONTH,1);  // will reset to first day of current month
String currentMonthFirstDay=dateFormat.format(cal.getTime());

cal.set(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));

String currentMonthLastDay=dateFormat.format(cal.getTime());
  
First & Last Date of previous Month

cal.add(java.util.Calendar.MONTH,-1); // will reduce one month time
              
cal.set(java.util.Calendar.DAY_OF_MONTH,1);  // will reset to first day of previous month
String previousMonthFirstDay=dateFormat.format(cal.getTime());

cal.add(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)-1);
String previousMonthLastDay=dateFormat.format(cal.getTime());

  
First and Last Date of Year

cal.set(java.util.Calendar.DAY_OF_YEAR, 1);  //will reset to first day of year
String yearFirstDay=dateFormat.format(cal.getTime());

cal.set(java.util.Calendar.DAY_OF_YEAR, cal.getActualMaximum(java.util.Calendar.DAY_OF_YEAR));
String yearLastDay=dateFormat.format(cal.getTime());

  
  
Sample Program

DateSample.java

package com;

//Author: Chandrasekhar Atina
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateSample {

public static void main(String[] args) {

String currentWeekFirstDay;
String currentWeekLastDay;
String previousWeekFirstDay;
String previousWeekLastDay;
String currentMonthFirstDay;
String currentMonthLastDay;
String previousMonthFirstDay;
String previousMonthLastDay;
String yearFirstDay;
String yearLastDay;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Calendar cal = java.util.Calendar.getInstance();

cal.set(java.util.Calendar.DAY_OF_WEEK,1);
currentWeekFirstDay=dateFormat.format(cal.getTime());
cal.set(java.util.Calendar.DAY_OF_WEEK,7);
currentWeekLastDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.WEEK_OF_YEAR,-1);
cal.set(java.util.Calendar.DAY_OF_WEEK,1);
previousWeekFirstDay=dateFormat.format(cal.getTime());
cal.set(java.util.Calendar.DAY_OF_WEEK,7);
previousWeekLastDay=dateFormat.format(cal.getTime());
          
cal = java.util.Calendar.getInstance(); // resetting the calendar instance
          
cal.set(java.util.Calendar.DAY_OF_MONTH,1);
currentMonthFirstDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)-1);
currentMonthLastDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.MONTH,-1);
cal.set(java.util.Calendar.DAY_OF_MONTH,1);
previousMonthFirstDay=dateFormat.format(cal.getTime());
cal.add(java.util.Calendar.DAY_OF_MONTH,cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)-1);
previousMonthLastDay=dateFormat.format(cal.getTime());

cal = java.util.Calendar.getInstance(); // resetting the calendar instance

cal.set(java.util.Calendar.DAY_OF_YEAR, 1);
yearFirstDay=dateFormat.format(cal.getTime());
cal.set(java.util.Calendar.DAY_OF_YEAR, cal.getActualMaximum(java.util.Calendar.DAY_OF_YEAR));
yearLastDay=dateFormat.format(cal.getTime());
      
System.out.println("First Day of Current Week  : "+currentWeekFirstDay);
System.out.println("Last Day of Current Week   : "+currentWeekLastDay);
System.out.println("First Day of Previous Week : "+previousWeekFirstDay);
System.out.println("Last Day of Previous Week  : "+previousWeekLastDay);
System.out.println("First Day of Current Month : "+currentMonthFirstDay);
System.out.println("Last Day of Current Month  : "+currentMonthLastDay);
System.out.println("First Day of Previous Month: "+previousMonthFirstDay);
System.out.println("Last Day of Previous Month : "+previousMonthLastDay);
System.out.println("First Day of Current Year  : "+yearFirstDay);
System.out.println("Last Day of Current Year   : "+yearLastDay);
}
}

 
Output

No comments:

Post a Comment