找到 34423 篇文章 关于编程

如何在 Java 中检查给定日期是否为周末

Nancy Den
更新于 2019-07-30 22:30:25

1K+ 次浏览

首先,显示当前日期:LocalDate date = LocalDate.now();现在,从上述日期(当前日期)获取星期几:DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));根据上述结果,使用 SWITCH 检查当前日期。如果日期是星期六/星期日,则为周末。示例import java.time.DayOfWeek; import java.time.temporal.ChronoField; import java.time.LocalDate; public class Demo {    public static void main(String[] argv) {       LocalDate date = LocalDate.now();       DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));       switch (day) {          case SATURDAY:             System.out.println("Weekend - Saturday");         ... 阅读更多

如何在两个 Java LocalDate 之间获取天、月和年?

Nancy Den
更新于 2019-07-30 22:30:25

894 次浏览

设置两个 Java 日期:LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29);现在,使用 Period 类中的 between() 方法获取两个日期之间的差值:Period p = Period.between(date1, date2);现在,获取年份、月份和天数:p.getYears() p.getMonths() p.getDays()示例import java.time.LocalDate; import java.time.Period; public class Demo {    public static void main(String[] args) {       LocalDate date1 = LocalDate.of(2019, 3, 25);       LocalDate date2 = LocalDate.of(2019, 4, 29);       System.out.println("Date 1 = "+date1);       System.out.println("Date 2 = "+date2);       Period p = Period.between(date1, date2);       System.out.println("Period = "+p);   ... 阅读更多

Java 程序向 LocalDate 添加时间段

Nancy Den
更新于 2024-11-08 22:29:57

455 次浏览

在这个程序中,我们将学习如何使用 Java 的 LocalDate 类向日期添加特定时间段。通过使用 Java 的 Period 类,我们可以指定一段时间(例如月份和天数),然后将此时间段添加到 LocalDate。该程序演示了如何设置时间段并将其应用于给定日期以获取新的、更新的日期。向 LocalDate 添加时间段的步骤以下是向 LocalDate 添加时间段的步骤 - 从 java.time 包中导入 LocalDate 和 Period 以处理日期和时间段。 ... 阅读更多

Java 程序从 Instant 中减去秒和纳秒

Daniol Thomas
更新于 2019-07-30 22:30:25

211 次浏览

让我们首先设置一个 Instant:Instant now = Instant.ofEpochMilli(184142540078l);现在让我们从 Instant 中减去秒:Instant resSeconds = now.minusSeconds(50);现在让我们从 Instant 中减去纳秒:Instant resNanoSeconds = now.minusNanos(10000);示例import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant now = Instant.ofEpochMilli(184142540078l);       System.out.println(now);       Instant resSeconds = now.minusSeconds(50);       System.out.println("减去秒后 = "+resSeconds);       Instant resNanoSeconds = now.minusNanos(10000);       System.out.println("减去纳秒后 = "+resNanoSeconds);    } }输出1975-11-02T06:42:20.078Z 减去秒后 = 1975-11-02T06:41:30.078Z 减去纳秒后 = 1975-11-02T06:42:20.077990Z

如何在 Java 中获取两个 Instant 时间戳之间的秒和分钟

Krantik Chavan
更新于 2019-07-30 22:30:25

3K+ 次浏览

以下是两个 Instant 时间戳:Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935);获取两个 Instant 之间的 Duration:Duration res = Duration.between(one, two);现在,获取两个时间戳之间的秒数:long seconds = res.getSeconds();现在,获取两个时间戳之间的分钟数:long minutes = res.abs().toMinutes();示例import java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant one = Instant.ofEpochSecond(1355836728);       Instant two = Instant.ofEpochSecond(1355866935);       Duration res = Duration.between(one, two);       System.out.println(res);       long seconds = res.getSeconds();       System.out.println("Duration 之间的秒数 = "+seconds);   ... 阅读更多

如何在 Java 中获取两个 Instant 时间戳之间的持续时间

Krantik Chavan
更新于 2019-07-30 22:30:25

19K+ 次浏览

让我们首先使用从 1970-01-01T00:00:00Z 纪元开始的秒数,使用 ofEpochSeconds() 方法设置两个 Instant。现在获取上述两个 Instant 之间的持续时间:Duration res = Duration.between(one, two);示例import java.time.Duration; import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant one = Instant.ofEpochSecond(1845836728);       Instant two = Instant.ofEpochSecond(1845866935);       Duration res = Duration.between(one, two);       System.out.println(res);    } }输出PT8H23M27S

Java 程序从 Instant 获取毫秒

Krantik Chavan
更新于 2019-07-30 22:30:25

191 次浏览

创建一个 Instant 并使用作为参数传递的毫秒数从 1970-01-01T00:00:00Z 纪元开始获取一个 Instant。这是使用 ofEpochMilli() 完成的:Instant instant = Instant.ofEpochMilli(342627282920l);现在,从 Instant 获取纪元毫秒:instant. toEpochMilli();示例import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(1262347200000l);       long res = instant.toEpochMilli();       System.out.println(res);    } }输出1262347200000

Java 程序从 Instant 获取纪元秒

Krantik Chavan
更新于 2019-07-30 22:30:25

193 次浏览

创建一个 Instant 并使用作为参数传递的毫秒数从 1970-01-01T00:00:00Z 纪元开始获取一个 Instant。这是使用 ofEpochMilli() 完成的:Instant instant = Instant.ofEpochMilli(342627282920l);现在,从 Instant 获取纪元秒:instant.getEpochSecond();示例import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(342627282920l);       long res = instant.getEpochSecond();       System.out.println(res);    } }输出342627282

Java 程序从纪元秒和毫秒创建 Instant

Krantik Chavan
更新于 2019-07-30 22:30:25

1K+ 次浏览

从纪元秒创建 Instant示例import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochSecond(282829279);       System.out.println(instant);    } }输出1978-12-18T11:41:19Z从纪元毫秒创建 Instant示例import java.time.Instant; public class Demo {    public static void main(String[] args) {       Instant instant = Instant.ofEpochMilli(272827282728l);       System.out.println(instant);    } }输出1978-08-24T17:21:22.728Z

Java 程序将此持续时间转换为纳秒的总长度

Krantik Chavan
更新于 2019-07-30 22:30:25

123 次浏览

有了这个,获取天、小时和分钟的纳秒数。首先,设置持续时间:Duration d1 = Duration.ofDays(5); Duration d2 = Duration.ofHours(20); Duration d3 = Duration.ofMinutes(15);将上述持续时间转换为纳秒:System.out.println("5 天的纳秒数 = "+d1.toNanos()); System.out.println("20 小时的纳秒数 = "+d2.toNanos()); System.out.println("15 分钟的纳秒数 = "+d3.toNanos());示例import java.time.Duration; public class Demo {    public static void main(String[] args) {       Duration d1 = Duration.ofDays(5);       Duration d2 = Duration.ofHours(20);       Duration d3 = Duration.ofMinutes(15);       System.out.println("5 天的纳秒数 = "+d1.toNanos());       System.out.println("20 小时的纳秒数 = "+d2.toNanos());   ... 阅读更多

广告

© . All rights reserved.