Showing posts with label Calendar. Show all posts
Showing posts with label Calendar. Show all posts

Friday, 11 March 2011

Getting last day of month

public static int getLastDayOfMonth {
//
// Get a calendar instance
//
Calendar calendar = Calendar.getInstance();

//
// Get the last date of the current month.
// To get the last date for a
// specific month you can set the calendar month
//using calendar object
// calendar.set(Calendar.MONTH, theMonth) method.
//
int lastDate = calendar.getActualMaximum(Calendar.DATE);

//
// Set the calendar date to the last date of the month so then we can
// get the last day of the month
//
calendar.set(Calendar.DATE, lastDate);
int lastDay = calendar.get(Calendar.DAY_OF_WEEK);

//
// Print the current date and the last date of the month
//
System.out.println("Last Date: " + calendar.getTime());

//
// The lastDay will be in a value from 1 to 7 where 1 = monday and 7 =
// saturday respectively.
//
return lastDay;
}

Calculating difference between 2 dates

Again to do Date arithematic we need Calender class.

Following function takes 2 dates and prints difference in days, millis etc...you can make you own function to return long etc...

public static void printDiff(Date dat1, Date dat2)
{
// Creates two calendars instances
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

// Set the date for both of the calendar instance
cal1.setTime(dat1);
cal2.setTime(dat2);

// Get the represented date in milliseconds
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();

// Calculate difference in milliseconds
long diff = milis2 - milis1;

// Calculate difference in seconds
long diffSeconds = diff / 1000;

// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);

// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);

// Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.println("In milliseconds: " + diff + " milliseconds.");
System.out.println("In seconds: " + diffSeconds + " seconds.");
System.out.println("In minutes: " + diffMinutes + " minutes.");
System.out.println("In hours: " + diffHours + " hours.");
System.out.println("In days: " + diffDays + " days.");
}

Date arithematic


The java.util.Calendar allows us to do a date arithmetic function such as add or subtract a unit of time to the specified date field.
The method that done this process is the Calendar.add(int field, int amount). Where the value of the field can be Calendar.DATE, Calendar.MONTH, Calendar.YEAR. So this mean if you want to subtract in days, months or years use Calendar.DATE, Calendar.MONTH or Calendar.YEAR respectively.

import java.util.Calendar;

public class CalendarAddExample {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();

System.out.println("Today : " + cal.getTime());

// Subtract 30 days from the calendar
cal.add(Calendar.DATE, -30);
System.out.println("30 days ago: " + cal.getTime());

// Add 10 months to the calendar
cal.add(Calendar.MONTH, 10);
System.out.println("10 months later: " + cal.getTime());

// Subtract 1 year from the calendar
cal.add(Calendar.YEAR, -1)
System.out.println("1 year ago: " + cal.getTime());
}
}

Java Date API


java.util.
Class Date
java.lang.Object
extended by java.util.Date
All Implemented Interfaces:
Cloneable, Comparable, Serializable
Direct Known Subclasses:
Date, Time, Timestamp
public class Date extends Object
implements Serializable, Cloneable, Comparable
The class Date represents a specific instant with millisecond precision.

See here for more resources.

Some examples of using it:

Converting Date to stringConverting string to DateDate arithematic
Calculating difference between 2 dates
Getting last day of month