I looked at the new Date/Time in Java 8 and saw that they made it much easier than before. So I thought of a thing to do with this new Date/Time and see if I could do it.
I want to give the program two different time and let the program look what time it is and tell me how many hours, min, sec is left to the specific time.
LocalTime time = LocalTime.now();
LocalTime morning = LocalTime.of(05, 00);
LocalTime evening = LocalTime.of(17, 00);
Now with this, I will have 2 different time 5AM, 5PM plus the time for now. How do I now take out the hour and min of the now time and compare to morning, evening and see how many hours, mins is left to one of them?
I tried this:
long betweenMor = ChronoUnit.HOURS.between(morning, time) + ChronoUnit.MINUTES.between(morning, time);
long betweenEve = ChronoUnit.HOURS.between(evening, time) + ChronoUnit.MINUTES.between(evening, time);
With that I will have the hours and mins between time and morning, and time and evening. How do I do now? The tricky part for me is that IF the time is BEFORE 5PM/17.00, then the program will tell me how many hours, min is left, but if the time is 17:01-17:59 the time will say just the mins, it will not count from 5PM to 5AM. It will not say, its 11 hours 25 min left ..
And the last question, can I make the time realtime? I mean, I can print out time, and I will see the hours, min, sex, nanosec.. but it will not update, i need to restart the program to get the new time ..is it any way I can watch the time in real time? So I see the nanosec, seconds go up or down.. ?
Thanks.
If you use the base time points, LocalTime.of(05, 00)
and LocalTime.of(17, 00)
you are using times of today. If I understand you correctly, you want calculate duration relative to the closest points which implies that if evening has passed, you would calculate the time relative to tomorrow’s morning or relative to yesterday’s evening if midnight has passed, i.e. when your current time is before morning.
You can do it like this:
LocalTime morning = LocalTime.of(05, 00);
LocalTime evening = LocalTime.of(17, 00);
LocalTime time = LocalTime.now();
Duration fromMorning = Duration.between(morning, time);
Duration toEvening = Duration.between(evening, time);
if(fromMorning.isNegative()) toEvening=toEvening.plusDays(1);
else if(!toEvening.isNegative()) fromMorning=fromMorning.minusDays(1);
System.out.println(format(fromMorning, "morning"));
System.out.println(format(toEvening, "evening"));
using the following helper method:
private static String format(Duration d, String event) {
long s=d.getSeconds();
String prep=" since ";
if(s<0) { s=-s; prep=" to "; }
long h=s/3600, min=s/60;
s-=min*60; min-=h*60;
return h+" hour(s), "+min+" min, "+s+" sec"+prep+event;
}
If you want to update the time, it’s a matter of your program to recalculate these values and use a proper display technique to show updated values. E.g. a simple swing based application might look like this:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalTime;
import javax.swing.*;
public class Clock {
public static void main(String... arg) {
EventQueue.invokeLater(Clock::openGUI);
}
static void openGUI()
{
try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch(Exception ex){}// not mandatory
JFrame frame=new JFrame("Clock");
JLabel l1=new JLabel(), l2=new JLabel();
ActionListener updater=ev->{
LocalTime morning = LocalTime.of(05, 00);
LocalTime evening = LocalTime.of(17, 00);
LocalTime time = LocalTime.now();
Duration fromMorning = Duration.between(morning, time);
Duration toEvening = Duration.between(evening, time);
if(fromMorning.isNegative()) toEvening=toEvening.plusDays(1);
else if(!toEvening.isNegative()) fromMorning=fromMorning.minusDays(1);
l1.setText(format(fromMorning, "morning"));
l2.setText(format(toEvening, "evening"));
};
updater.actionPerformed(null);
frame.getContentPane().add(l1, BorderLayout.NORTH);
frame.getContentPane().add(l2, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new Timer(1000, updater).start();
}
private static String format(Duration d, String event) {
long s=d.getSeconds();
String prep=" since ";
if(s<0) { s=-s; prep=" to "; }
long h=s/3600, min=s/60;
s-=min*60; min-=h*60;
return h+" hour(s), "+min+" min, "+s+" sec"+prep+event;
}
}