简体   繁体   中英

How to set Calendar object to current date but time from SimpleDateFormat that contains HH:mm:ss

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.?

tl;dr

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. 
    )
)

java.time

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. 

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]));

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM