Date Time Calendar in Java

Converting Strings to Date Object

There is one method in Java which can convert a String to a Date. But, the string which are trying to convert to a date, must be in a proper format. If not, then the code will throw an exception.

Now, what is an Exception! Never heard of that!

Yeah… it is a huge topic in Java. We will discuss that in another chapter very soon. But, for now you can think of it as an examiner in Java.

It checks if a particular section of code is properly executable by Java. If not, it informs JVM about it.

So, here is the sample code for converting String to Date.

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateDemo {

	public static void main(String[] args) throws ParseException {
		// String which we want to convert to Date
		String dateString = "2015-02-02 06:34:45";	
		// format we provide for date format
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		// converts the string to date and prints it in console
		System.out.println(format.parse(dateString));
	}
}

The output for this code is:

Mon Feb 02 06:34:45 CET 2015

Now, obviously as you can understand we can use different formats to convert a String in a Date using the table we mentioned in the last chapter.

Convert time from one time zone to another

How is that if I am sitting in Germany, and I want to know the corresponding time in India and Bangkok! (No, I am not saying about Microsoft widgets!)

We can do it programmatically to! Just see the below program:

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

public class ConvertTimeZone {

	public static void main(String[] args) throws ParseException {
		DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
		
		// creating a new date of in my current time zone which is CET
		Date date = new Date();
		System.out.println(formatter.format(date));

		// Set the formatter to use a different time zone - Indian Standard Time
		formatter.setTimeZone(TimeZone.getTimeZone("IST"));
		System.out.println("IST time : " + formatter.format(date));

		// Set the formatter to use a different time zone - Bangkok Time zone
		formatter.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
		System.out.println("ICT time : " + formatter.format(date));
	}

}

The output will be :

Note: when we set the time zone manually using

formatter.setTimeZone(TimeZone.getTimeZone("IST"));

Java automatically converts the time to that time zone and displays it!

GregorianCalendar Class

This is the Java class which is a concrete and complete implementation of Date and time in Java.

This class provides an implementation of all the abstract methods in Calendar class and also provides some additional methods.

There are a number of constructors and methods in GregorianCalendar class.

We can use any of these above mentioned methods to create a GregorianCalendar Objects.

Some important methods in GregorianCalendar class

 

GregorianCalendar Class in Java is simply huge? For complete list of all the methods please refer the Oracle documentation of GregorianCalendar Class in Java.


Next > < Prev
Scroll to Top