[英]Take out the hour, min and second in new Date/Time in Java 8
我查看了Java 8中的新日期/时间,发现它们比以前更容易了。 因此,我想到了与此新日期/时间相关的内容,看看是否可以这样做。
我想给程序两个不同的时间,让程序看看现在是什么时间,然后告诉我指定时间还剩下几小时,最少,几秒钟。
LocalTime time = LocalTime.now();
LocalTime morning = LocalTime.of(05, 00);
LocalTime evening = LocalTime.of(17, 00);
现在,我将有2个不同的时间,分别是5 AM、5PM和现在的时间。 我现在如何取出现在时间的小时和分钟数,并与早晨,傍晚进行比较,看看其中剩下的小时数和分钟数是多少?
我尝试了这个:
long betweenMor = ChronoUnit.HOURS.between(morning, time) + ChronoUnit.MINUTES.between(morning, time);
long betweenEve = ChronoUnit.HOURS.between(evening, time) + ChronoUnit.MINUTES.between(evening, time);
这样,我将有时间和早晨之间以及时间和晚上之间的时间和分钟。 我现在该怎么办? 对我来说,最棘手的部分是,如果时间在5 PM/17.00之前,那么程序将告诉我还剩多少小时,但如果时间是17:01-17:59,则时间只会显示分钟,从下午5点到凌晨5点不算。 它不会说,它还有11小时25分钟。
最后一个问题,我可以实时显示时间吗? 我的意思是,我可以打印出时间,然后我会看到小时,分钟,性别,纳秒..但它不会更新,我需要重新启动程序以获取新的时间..这是我可以观看的任何方式吗?实时时间? 所以我看到了纳秒级,秒数上升或下降。
谢谢。
如果使用基准时间点LocalTime.of(05, 00)
LocalTime.of(17, 00)
LocalTime.of(05, 00)
和LocalTime.of(17, 00)
,则使用的是今天的时间 。 如果我对您的理解正确,则需要计算相对于最接近点的持续时间,这意味着如果傍晚过去了,那么您将计算相对于明天早晨的时间,如果过去了午夜,则将计算相对于昨天傍晚的时间,即当前时间在早晨之前。
您可以这样做:
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"));
使用以下辅助方法:
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;
}
如果要更新时间,则程序要重新计算这些值,并使用适当的显示技术来显示更新的值。 例如,一个简单的基于swing的应用程序可能如下所示:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.