Thursday, February 9, 2017

Java, How to convert Calendar to String?, How to convert String to Calendar? and How to compare days, weeks and years with calendar?

Hello everybody, in this post I will show you how you can convert calendar to string, string to calendar and how you can compare days, weeks and years using java calendar library.

This is the code:


 import java.text.ParseException;   
 import java.text.SimpleDateFormat;   
 import java.util.Calendar;   
 public class DatesUtilities{   
    private Calendar dateTimeNow = null;   
    private SimpleDateFormat sdf = null;   
    public DatesUtilities(){   
       dateTimeNow = Calendar.getInstance();   
       sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
    }   
    /*This method compare two dates, by the days, months and the years*/   
    public boolean equalsDates(Calendar firstDate, Calendar secondDate) throws Exception {   
       if(firstDate.get(Calendar.DAY_OF_MONTH) != secondDate.get(Calendar.DAY_OF_MONTH)){   
         return false;   
       }   
       if(firstDate.get(Calendar.MONTH) != secondDate.get(Calendar.MONTH)){   
         return false;   
       }   
       if(firstDate.get(Calendar.YEAR) != secondDate.get(Calendar.YEAR)){   
         return false;   
       }   
       return true;   
    }   
    /*This method receive two dates for compare like the previews method, but one of them is a string*/   
    public boolean equalsDates(Calendar firstDate, String secondDate) throws Exception {   
       return equalsDates(firstDate, stringToCalendar(secondDate));   
    }   
    /*This method return a calendar date from a string date*/   
    public Calendar stringToCalendar(String date) throws ParseException{        
       Calendar calendarDate = Calendar.getInstance();   
       calendarDate.setTime(sdf.parse(date));        
       return calendarDate;        
    }   
     /*This method return a String from a calendar using SimpleDateFormat*/  
      public String calendarToString(Calendar date) throws Exception{  
           String stringDate = sdf.format(date.getTime());  
           return stringDate;  
      }  
    /*This method return a Calendar with the current date*/   
    public Calendar getDateTimeNow() {   
       return dateTimeNow;   
    }   
    /*This method return a string with the current date from a Calendar*/   
    public String getDateTimeNowString(){        
       return sdf.format(dateTimeNow.getTime());   
    }   
  }   


First we use SimpleDateFormat to tell the system how is the date format that we will use. In this example we'll use year-Month-day hour:minutes:seconds.

 sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  

We start the variable dateTimeNow with the current date:

 dateTimeNow = Calendar.getInstance();  


Now we use the method getDateTimeNow for return the current date:

 /*This method return a Calendar with the current date*/  
      public Calendar getDateTimeNow() {  
           return dateTimeNow;  
      }  


We use the getDateTimeNow method for return the current date like a String from calendar:

 /*This method return a string with the current date from a Calendar*/  
      public String getDateTimeNowString(){            
           return sdf.format(dateTimeNow.getTime());  
      }  
Look that we are using the SimpleDateFormat.format method.


We use the stringToCalendar method for convert a String type date into Calendar type:

 /*This method return a calendar date from a string date*/  
      public Calendar stringToCalendar(String date) throws ParseException{            
           Calendar calendarDate = Calendar.getInstance();  
           calendarDate.setTime(sdf.parse(date));            
           return calendarDate;            
      }  
Look that we are using the SimpleDateFormat.parse method.


We use the calendarToString method for convert a Calendar type into String type with a custom format:

 /*This method return a String from a calendar using SimpleDateFormat*/  
      public String calendarToString(Calendar date) throws Exception{  
           String stringDate = sdf.format(date.getTime());  
           return stringDate;  
      }  
Look that we are using the SimpleDateFormat.format method.


We use the equalsDate method for compare two dates, in this example we compare if the days, months and years are equals, the method returns false if any of them are not the same:

 /*This method compare two dates, by the days, months and the years*/  
      public boolean equalsDates(Calendar firstDate, Calendar secondDate) throws Exception {  
           if(firstDate.get(Calendar.DAY_OF_MONTH) != secondDate.get(Calendar.DAY_OF_MONTH)){  
                return false;  
           }  
           if(firstDate.get(Calendar.MONTH) != secondDate.get(Calendar.MONTH)){  
                return false;  
           }  
           if(firstDate.get(Calendar.YEAR) != secondDate.get(Calendar.YEAR)){  
                return false;  
           }  
           return true;  
      }  


And the "public boolean equalsDates(Calendar firstDate, String secondDate)" is an overloaded method that receive two variables, the first variable is a calendar type and the second variable is a String, this method use "stringToCalendar" method to convert the String type into Calendar type and latter it use the previous method to compare both days:

 /*This method receive two dates for compare like the previews method, but one of them is a string*/  
      public boolean equalsDates(Calendar firstDate, String secondDate) throws Exception {  
           return equalsDates(firstDate, stringToCalendar(secondDate));  
      }  


If you have any coment, you can write it down here:


No comments:

Post a Comment