Reputation: 35
I need to create a new Calendar
object that contains the current date but the time needs to be set from a given String of format HH:mm:ss
.
I create a new calendar object with current date and time and then use a SimpleDateFormat
object to parse the string and set the time from that one but that only overwrites the calendar object with the parsed time and Jan 1 1970:
def currentTime = new java.util.Date();
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(currentTime);
java.util.Date inTime = new SimpleDateFormat("HH:mm:ss").parse(initialTime);
calendar1.setTime(inTime);
Is there a way to get the values of Hour, Minute, Seconds and Milliseconds from the Date object to use it with calendar.set(Calendar.HOUR_OF_DAY, hour)
, etc.?
Upvotes: 2
Views: 12131
Reputation: 338201
GregorianCalendar.from( // Converting from modern java.time class to troublesome legacy class. Do so only if you must. Otherwise use only the java.time classes.
ZonedDateTime.of( // Modern java.time class representing a moment, a point on the timeline, with an assigned time zone through which to see the wall-clock time used by the people of a particular region.
LocalDate.now( ZoneId.of( “Pacific/Auckland” ) ) , // The current date in a particular time zone. For any given moment, the date varies around the globe by zone.
LocalTime.of( 12 , 34 , 56 ) , // Specify your desired time-of-day.
ZoneId.of( “Pacific/Auckland” ) // Assign a time zone for which the date and time is intended.
)
)
The modern approach uses the java.time classes.
ZoneId z = ZoneId.of( “America/Montreal” ) ;
LocalDate ld = LocalDate.now( z ) ;
LocalTime lt = LocalTime.of( 12 , 34 , 56 ) ; // 12:34:56
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
You can extract the time-of-day (or date) from an existing ZonedDateTime
.
LocalTime lt = zdt.toLocalTime() ;
LocalDate ld = zdt.toLocalDate() ;
Best to avoid the troublesome old legacy date-time classes added before Java 8. But if you must, you can convert between the modern and legacy classes. Call on new methods added to the old classes.
GregorianCalendar gc = GregorianCalendar.from( zdt ) ; // If you must, but better to avoid the troublesome old legacy classes.
Upvotes: 3
Reputation: 21
Not sure if this helps you.
String hhmmss = "10:20:30";
String[] parts = hhmmss.split(":");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(parts[1]));
cal.set(Calendar.SECOND, Integer.parseInt(parts[2]));
Upvotes: 2
Reputation: 1748
Calendar objet Time is a java.util.Date object with the standard format. You can not set date with a specific format to your calendar.
To get the Date details (Hours, Minutes ...) try :
final Date date = new Date(); // your date
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int year = cal.get(Calendar.YEAR);
final int month = cal.get(Calendar.MONTH);
final int day = cal.get(Calendar.DAY_OF_MONTH);
final int hour = cal.get(Calendar.HOUR_OF_DAY);
final int minute = cal.get(Calendar.MINUTE);
final int second = cal.get(Calendar.SECOND);
Upvotes: 1