Wednesday, October 5, 2011

JAVA: Date and Calendar

This is not something new but something of very common use in most of the projects. In this post I am going to write some date, time and calendar functions that are commonly used as utility. From next time I will copy them from this post rather than code them again.

To get a Date in any Format the SimpleDateFormat Class is used. Following snippet will return current date in provided format in SimpleDateFormat argument.

Date date = new Date();    
DateFormat dateFormatter = new SimpleDateFormat("dd");    
String formattedDate = dateFormatter.format(date);

Format "dd" is used to get the date number from date

Format "yyyy" is used to get the year number from date

Format "MM" is used to get the month number from date

Format "MMMM" is used to get the month name from date

This method will return the date of targeted format from source format.

    String dt = dateString;  // Start date
    SimpleDateFormat sdf = null;
    try {
            sdf = new SimpleDateFormat(sourceFormat);
            Date date = sdf.parse(dt);
            sdf  = new SimpleDateFormat(targetFormat);
            dt = sdf.format(date);  // dt is now in the new date format            
    } catch (ParseException e) {

To get Number of Days in a Month we will take help from Calendar Class here. Following method will be usd to get number of days in a month.

public static int getNumberOfDaysInMonth(int screenMonth){
      Calendar calendar = Calendar.getInstance();
      int year = Integer.valueOf(getYearNumber());
      int month = screenMonth;
      int date = 1;
      calendar.set(year, month, date);
      int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);        
      return days;

This method will return Month Name from month number. Calendar is used here as well.

public static String getMonthNameByMonthNumber(int currentMonthNumber) {
    String dt;
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, currentMonthNumber);
    c.add(Calendar.MONTH, 0);  // number of days to add
    SimpleDateFormat sdf  = new SimpleDateFormat("MMMM");
    dt = sdf.format(c.getTime());  // dt is now the new date        
    return dt;

To get the First Day of month following method can be used.

public static String getFirstDayOfMonth(int month) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DATE, 1);

    DateFormat f = new SimpleDateFormat("EEE");        
    return f.format(c.getTime());

Similarly to get the first date of week Calendar Class is used.

public static int getFirstDateOfWeek() {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());

    DateFormat f = new SimpleDateFormat("dd");
    String cc = f.format(c.getTime());

    // Plus 1 is done to get the Monday Date instead of Sunday
    return Integer.valueOf(cc) + 1;

There are many other methods that can be used commmonly. I will keep updating this post when ever I got something new. Also do comment If there is any method you want to add here or If any of them can be implemented in much better way.


No comments: