Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the time and only the time in a calendar in Java?

Having the hours and minutes, is there any easier or better way to set it into a Calendar object than:

calendar.set(calendar.get(Calendar.YEAR),                         calendar.get(Calendar.MONTH),                         calendar.get(Calendar.DAY_OF_MONTH),                         hour, minute); 
like image 837
pupeno Avatar asked Jan 20 '09 06:01

pupeno


People also ask

How do I set the time on my Java calendar?

Calendar setTime() Method in Java with ExamplesThe setTime(Date dt) method in Calendar class is used to set Calendars time represented by this Calendar's time value, with the given or passed date as a parameter. Parameters: The method takes one parameter dt of Date type and refers to the given date that is to be set.

Can you format calendar in Java?

You can format the calender date object when you want to display and keep that as a string.

What is Calendar getInstance () in Java?

The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.

Does Java calendar have TimeZone?

The java. util. Calendar. getTimeZone() method gets the time zone.

How to set the time of current calendar object in Java?

The setTime () method of java.util.Calendar class is used to set the Time of current calendar object. A Date object id is passed as the parameter into this method. date - a date object. Does not return value.

How to use date and time in Java?

Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes. For example: Class. Description. LocalDate. Represents a date (year, month, day (yyyy-MM-dd)) LocalTime. Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns))

What is Settime in calendar in Java?

Calendar setTime () Method in Java with Examples Last Updated : 06 Mar, 2019 The setTime (Date dt) method in Calendar class is used to set Calendars time represented by this Calendar’s time value, with the given or passed date as a parameter.

How do you set the time in a calendar?

The setTime (Date dt) method in Calendar class is used to set Calendars time represented by this Calendar’s time value, with the given or passed date as a parameter. Parameters: The method takes one parameter dt of Date type and refers to the given date that is to be set.


2 Answers

From here:

calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); 
like image 98
Xn0vv3r Avatar answered Sep 21 '22 19:09

Xn0vv3r


Use set(int field, int value). The first parameter is the field number for HOUR/MINUTE/SECOND.

like image 24
Rahul Avatar answered Sep 20 '22 19:09

Rahul