Calendar set hour,minute,second

In this chapter you will learn:

  1. Set hour, minute and second for a Calendar

Set hour, minute and second for a Calendar

import java.util.Calendar;
//from  j  a  v  a 2 s .c o  m
public class Main {
  public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec" };

    Calendar calendar = Calendar.getInstance();

    // Set the time and date information and display it.
    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 29);
    calendar.set(Calendar.SECOND, 22);

    System.out.print("Updated time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));
  }

}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Add minutes to Calendar
  2. Subtract months to current date
  3. Add or subtract seconds to current time
  4. Add and subtract year from Calendar
  5. Calendar class adjusts the date when adding or subtracting months