java Calendar class (time set, comparison, format)

Set any time in the instance of Calendar class


Calendar tempTime = Calendar.getInstance();
            
 //Set time
 tempTime.set(Calendar.HOUR_OF_DAY, trainHour);
 tempTime.set(Calendar.MINUTE, trainTime);

Compare if A is before B


boolean isBefore = false;
//Whether tempTime is before safeTime
 isBefore  = tempTime.before(safeTime);

Format and output to "HH: mm"

import java.text.SimpleDateFormat;

---------------------------------------

//Create an instance of the calendar class
Calendar ansTime = Calendar.getInstance();

Date time = ansTime.getTime();
        
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

System.out.println(sdf.format(time));

Recommended Posts

java Calendar class (time set, comparison, format)
Java (set)
[Java] How to set the Date time to 00:00:00
[Java] How to use the Calendar class
[Java] Class inheritance
java Scanner class
Java HashMap class
[Java] Set the time from the browser with jsoup
Java anonymous class
About Java class
Java framework comparison
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[java] abstract class
[Java] Object class
Java local class
[Java] How to use Calendar class and Date class
[Java] Get the date 10 days later with the Calendar class
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Java version notation comparison
Format XML in Java
Java inner class review
Java class type field
Java Gold Countermeasures: Format
[Development] Java framework comparison
Java programming (class structure)
About java abstract class
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Java] Set structure of collection class (about HashSet and TreeSet)