How can I get the number of hours since the start of January 1 of the current year in Java? I. e. first hour of January 1 = 0001 Can I accomplish this with JodaTime or any other lib? Thanks,
Using the java.time package built into Java 8 and later (Tutorial):
ChronoUnit.HOURS.between(
LocalDateTime.of(LocalDateTime.now().getYear(), 1, 1, 0, 0),
LocalDateTime.now())
I think this is self-explenatory
Use the Java 8 API for date/time
LocalDateTime hournow = LocalDateTime.now();
LocalDateTime startOfYear = LocalDateTime.of(hournow.getYear(), 1, 1, 0, 0);
long hoursBetween = ChronoUnit.HOURS.between(startOfYear, hournow);
System.out.println("hours between: " + hoursBetween);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With