I'm getting the current time and I want to convert it into Integer, thats what I did , but it keeps giving me java.lang.NumberFormatException: For input string: "4:12:31"
long unixTime = System.currentTimeMillis() / 1000L;
Date time=new java.util.Date((long)unixTime*1000);
Calendar cal = Calendar.getInstance();
cal.setTime(time);
int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int currentTime = (int) Double.parseDouble((hours+":"+minutes+":"+seconds));
You cannot do that. If you need to compare times, use LocalTime.
LocalTime b = LocalTime.of(17, 30);//hh,mm,ss
LocalTime c = LocalTime.parse("20:30:30");//hh:mm:ss
System.out.println(b.isBefore(c));//true, also there are methods like compareTo, isAfter
If you need to compare dates, there is also a class called LocalDate
. If you need to compare both dates and time, you can use LocalDateTime
. Comparing those objects is the same, as comparing two LocalTime
obejcts.
LocalTime.of
and .parse
are static factory methods. They are used to create new instances of LocalTime
. From a user perspective, they work like constructors — you call them and you receive in return an object of its class. More about them and constructors you can read here: Java Constructors vs Static Factory Methods .
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.