【问题标题】:Take out the hour, min and second in new Date/Time in Java 8在 Java 8 的新日期/时间中取出小时、分钟和秒
【发布时间】:2015-06-06 01:53:48
【问题描述】:

我查看了 Java 8 中的新日期/时间,发现它们比以前更容易。所以我想到了用这个新的日期/时间做点什么,看看我能不能做到。

我想给程序两个不同的时间,让程序看看现在几点,告诉我具体时间还剩多少小时、分钟、秒。

LocalTime time = LocalTime.now();
LocalTime morning = LocalTime.of(05, 00);
LocalTime evening = LocalTime.of(17, 00);

现在有了这个,我将有 2 个不同的时间上午 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.now()而不是变量,要么在定时器上做一些事情,比如每100毫秒(每秒十次)。正如 Jon Skeet 所说,举出你所期望的例子,也许像代码一样表格

标签: java java-8 localtime


【解决方案1】:

如果您使用基本时间点,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;
}

如果您想更新时间,您的程序需要重新计算这些值并使用适当的显示技术来显示更新的值。例如。一个简单的基于 的应用程序可能如下所示:

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;
    }
}

【讨论】:

  • 我不得不说你真是太棒了。这段代码确实有效,这正是我所追求的。现在我需要坐下来看看你到底做了什么:) 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 2021-09-17
  • 2018-12-25
  • 2021-12-31
  • 2016-05-28
  • 2021-12-27
  • 2012-05-24
  • 1970-01-01
相关资源
最近更新 更多