Example of java date set time.

Example of java date set time.


Posted in : Core Java Posted on : November 29, 2010 at 3:51 PM Comments : [ 0 ]

Example of java date set time.

Example of java date set time.

In this example, you will see how to set time in java. The set(int field, int value) is a method of java Calendar class. It sets the value of given calendar field.

Code:

JavaSetTime.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class JavaSetTime {
public static void main(String[] arr) {
	Calendar cuCal = Calendar.getInstance();
	SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
	System.out.println("Current date : "
		+ dateFormat.format(cuCal.getTime()));
	cuCal.set(Calendar.HOUR, 4);
	System.out.println("After set method date : "
	+ dateFormat.format(cuCal.getTime()));
    }
}

Output 

Current date : 02:11:41 PM
After set method date : 04:11:41 PM

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics