在Java8中,去掉新日期/时间中的小时、分钟和秒
我查看了Java8中的新日期/时间,发现它们比以前简单多了。所以我想了一个新的日期/时间,看看我能不能做到 我想给程序两个不同的时间,让程序看看现在是什么时间,告诉我还有多少小时,分钟,秒到具体时间在Java8中,去掉新日期/时间中的小时、分钟和秒,java,java-8,localtime,swing,Java,Java 8,Localtime,Swing,我查看了Java8中的新日期/时间,发现它们比以前简单多了。所以我想了一个新的日期/时间,看看我能不能做到 我想给程序两个不同的时间,让程序看看现在是什么时间,告诉我还有多少小时,分钟,秒到具体时间 LocalTime time = LocalTime.now(); LocalTime morning = LocalTime.of(05, 00); LocalTime evening = LocalTime.of(17, 00); 现在有了这个,我将有两个不同的时间上午5点,下午5点加上现在的
LocalTime time = LocalTime.now();
LocalTime morning = LocalTime.of(05, 00);
LocalTime evening = LocalTime.of(17, 00);
现在有了这个,我将有两个不同的时间上午5点,下午5点加上现在的时间。
我现在如何计算出现在时间的小时和分钟,并与早上和晚上进行比较,看看其中一个时间还剩多少小时,分钟
我试过这个:
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点/17.00之前,程序会告诉我还有多少小时,分钟,但是如果时间是17:01-17:59,时间只会说分钟,从下午5点到凌晨5点不算。它不会说,还有11小时25分钟
最后一个问题,我能把时间变成实时的吗?我的意思是,我可以打印时间,我会看到小时,分钟,性,纳秒。。但是它不会更新,我需要重新启动程序来获取新的时间。我可以通过任何方式实时观察时间吗?所以我看到纳秒级,秒数上升还是下降
谢谢。如果您使用基本时间点,
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"));
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;
}
}
使用以下帮助器方法:
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;
}
私有静态字符串格式(持续时间d,字符串事件){
长s=d.getSeconds();
String prep=“自”;
如果{
LocalTime上午=LocalTime.of(05,00);
LocalTime傍晚=LocalTime.of(17,00);
LocalTime=LocalTime.now();
Duration fromMorning=Duration.between(上午,时间);
持续时间toEvening=持续时间(晚上,时间);
如果(fromMorning.isNegative())toEvening=toEvening.plusDays(1);
如果(!toEvening.isNegative())fromMorning=fromMorning.minusDays(1),则为else;
l1.setText(格式(fromMorning,“morning”));
l2.setText(格式(toEvening,“晚间”);
};
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);
新计时器(1000,更新程序).start();
}
私有静态字符串格式(持续时间d,字符串事件){
长s=d.getSeconds();
String prep=“自”;
if(sIt)对我来说真的不清楚你想要实现什么。如果你能以一个简短但完整的程序的形式展示一些示例输入和预期输出,这将有很大的帮助。你也应该在每篇文章中只问一个问题。实时要么总是使用LocalTime.now()
不要使用变量,也不要使用计时器,而是说每100毫秒(每秒十次)。正如Jon Skeet所说,给出你期望的例子,也许是表格式的代码。我需要说你太棒了。这段代码确实有效,正是我想要的。现在我需要坐下来看看你到底做了什么:)非常感谢你。