标签: java-time

Java 8在一段时间后过期

无法弄清楚如何在 java 8(没有日历)中执行这个简单的方法:

 import java.time.Duration;
 import java.util.Date;
  [...]
/**
 * true if date + duration is before now()
 * @param date 
 * @param d
 * @return
 */
public static boolean isDateExpired (Date date, Duration d) {
 // how to do ?
}
Run Code Online (Sandbox Code Playgroud)

java java-8 java-time

2
推荐指数
1
解决办法
1215
查看次数

格式化程序和不带年份的本地化 LocalDate (JSR-310)

我想将 a 格式化LocalDate为本地化的日期和月份,没有年份。

我不知道如何创建这样的格式化程序。这:

val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
Run Code Online (Sandbox Code Playgroud)

产生例如7/20/17,并且较长的版本还包括年份。

java date date-formatting java-time

2
推荐指数
1
解决办法
1760
查看次数

日期的剩余天数未正确显示

好的,所以我创建了一个函数来显示距离未来某个日期的天数...直到天数超过 9 天为止都是正确的..如果超过,它似乎会显示随机的天数。 ..请参阅下面的我的代码:

   public String daysTillExpire() {
        String daysLeft = "";
        int position = 0 ;
        String inputDateString = UIDM.get(position).date;
        Calendar calCurr = Calendar.getInstance();
        Calendar day = Calendar.getInstance();


        try {
            day.setTime(new SimpleDateFormat("dd-MM-yyyy").parse(inputDateString));

        } catch (ParseException e) {
            e.printStackTrace();
        }

            if (day.after(calCurr)) {

                String noumberOfDays = "Days left: " + (day.get(Calendar.DAY_OF_MONTH) - (calCurr.get(Calendar.DAY_OF_MONTH)));

                daysLeft = UIDM.get(position).date + "\n(" + noumberOfDays+")" ;

            }

            else if (day.before(calCurr)) {
                daysLeft = "Key Expired";
            return daysLeft; }
        return daysLeft;
    }
Run Code Online (Sandbox Code Playgroud)

UIDM是一个包含信息的数据模型...String inputDateString …

android date date-difference android-date java-time

2
推荐指数
1
解决办法
319
查看次数

使用 TimeZone 的 Calendar.getInstance 与 new Date()

我在我的应用程序中编写了一个实用程序,它接受时区并返回一个日期(java.util.Date),如下所示 -

// Inside MyDateHelper.java
public static java.util.Date getTimeForTimeZone(String timeZoneId)
{
    final Date currentDate = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId)).getTime();
    return currentDate;
}
Run Code Online (Sandbox Code Playgroud)

我在互联网上进行了研究,发现 Date 中没有任何时区信息。相反,它是一个从 1970 年开始的长值的包装器。所以,我的问题是我是否必须获取当前发生事务的日期时间时刻以存储在日期字段(类型为 java.util.Date)中),这个实用程序有什么用吗?

出于所有实际目的,这两行代码是否实现了相同的目的?

transactionEntry.setTime(MyDateHelper.getTimeForTimeZone("UTC+7:00"));
Run Code Online (Sandbox Code Playgroud)

或者

transactionEntry.setTime(new Date());
Run Code Online (Sandbox Code Playgroud)

我还检查了新的 Java 8 Time API,但就我的目的而言,我认为这样做也是一种矫枉过正,并且没有取得更多成果new Date()-

Date date4 = new Date();
LocalDateTime localDate = date4.toInstant().atZone(ZoneId.of("UTC+07:00")).toLocalDateTime();
final ZonedDateTime dateWithZone = localDate.atZone(ZoneId.of("UTC+07:00"));
Date dateOutput = Date.from(dateWithZone.toInstant());
transactionEntry.setTime(dateOutput);
Run Code Online (Sandbox Code Playgroud)

整个目的是捕获时区事务发生的时间,但我只能将时间存储为 java.util.Date。无论交易发生在哪个时区,Date 都能够正确捕获时间的瞬间,对吧?有人可以证实这种理解是正确的吗?

java timezone datetime java-8 java-time

2
推荐指数
1
解决办法
2626
查看次数

按区域设置顺序获取工作日

我想看看我们是否可以按区域设置排列工作日。例如,在美国语言环境中,我们将从星期日开始,而法国将从星期一开始(就日历而言)。为了理解它,我正在制作一个闹钟应用程序,其中工作日的闹钟会在某些日子重复 -

在此输入图像描述

以下是我所看到的工作日未按顺序排列的

new DateFormatSymbols().getShortWeekdays();

0 = ""
1 = "Sun"
2 = "Mon"
3 = "Tue"
4 = "Wed"
5 = "Thu"
6 = "Fri"
7 = "Sat"

0 = ""
1 = "dim."
2 = "lun."
3 = "mar."
4 = "mer."
5 = "jeu."
6 = "ven."
7 = "sam."
Run Code Online (Sandbox Code Playgroud)

android locale calendar dayofweek java-time

2
推荐指数
1
解决办法
1410
查看次数

在 Android 上使用 java.time 时出现 NoClassDefFoundError

我的 Android 应用程序使用的 JAR 依赖于 中的一些类java.time,例如LocalTime,因此至少需要 Java 8。

网上的多个消息来源指出,更高版本的 Android 和工具链支持 Java 8。

我使用 gradle 构建我的应用程序,但不使用(或希望使用)Android Studio。我使用该插件的 3.1.4 版本(最新版本与 Gradle 4.4 兼容,该版本随撰写本文时最新的 Ubuntu LTS 18.04 一起提供)。最低 SDK 为 14,目标为 26。构建工具版本为 27.0.3。

该应用程序构建并安装得很好,但是当它尝试使用其中一个类时,它会崩溃并出现异常:

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalTime;

问题:

  • 是否java.time适用于支持 Java 8 的 Android 版本?
  • 如果是,最低 API 版本是多少?(我在Anbox上测试过,它支持API 25。)
  • 我还缺少什么吗?

android java-8 java-time

2
推荐指数
1
解决办法
226
查看次数

解析工作日不适用于德语区域

我正在尝试使用 java.time.format.DateTimeFormatter 来解析时间字符串,但在解析德语短日名称时遇到问题。

鉴于以下程序

import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.util.Locale;

var locale = Locale.forLanguageTag("de");
var dtf = new DateTimeFormatterBuilder()
        .appendOptional(DateTimeFormatter.ofPattern("eeee"))
        .appendOptional(DateTimeFormatter.ofPattern("eee"))
        .toFormatter(locale);
var input1 = DayOfWeek.TUESDAY.getDisplayName(TextStyle.FULL, locale);
var input2 = DayOfWeek.TUESDAY.getDisplayName(TextStyle.SHORT_STANDALONE, locale);
System.out.printf("input: %s, parsed: %s\n", input1, dtf.parse(input1));
System.out.printf("input: %s, parsed: %s\n", input2, dtf.parse(input2));
Run Code Online (Sandbox Code Playgroud)

我期望的输出是

input: Dienstag, parsed: {DayOfWeek=2},ISO
input: Di, parsed: {DayOfWeek=2},ISO
Run Code Online (Sandbox Code Playgroud)

但我实际上得到

input: Dienstag, parsed: {DayOfWeek=2},ISO
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Di' could not be parsed, unparsed text found at index 0
    at …
Run Code Online (Sandbox Code Playgroud)

java locale date-parsing java-time

2
推荐指数
1
解决办法
417
查看次数

如何使用 DateTimeFormatter 或 Java 中的任何其他库将“2019-07-14T18:30:00.000Z”转换为“2021-09-26 04:30:00 PM”

我不确定这个问题是否已经得到回答,但谁能告诉我如何使用“2019-07-14T18:30:00.000Z”将“2019-07-14 04:30:00 PM”转换为“2019-07-14 04:30:00 PM” DateTimeFormatter 或 Java 中的任何其他库?基本上输出日期时间应该有 AM/PM 格式的时间。

java datetime-format java-time

2
推荐指数
2
解决办法
12万
查看次数

如何根据 1 小时间隔获取时间段

我想将时间段存储在数组列表中。我有开始时间和结束时间。根据开始时间,它应该创建时间段。例如,如果开始时间是 09:00AM,结束时间是 21:00PM,那么它应该添加到 arraylist 中,如下所示

09:00AM 10:00AM 11:00AM 12:00PM 13:00PM 14:00PM ..... 等等 21:00PM

因此,一个用户预订了 13:00PM 至 15:00PM 的时段,因此该时段不应可供其他用户使用,并且其他时段应该可用。如何将已预订时间与新数组列表进行比较。

代码

    private void getStartHourArray() {

    times = new ArrayList<TimeSlot>();
    Calendar calender = Calendar.getInstance();

    calender.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));

    int ti = calender.get(Calendar.HOUR_OF_DAY);
    int minutes = calender.get(Calendar.MINUTE);

    System.out.println(minutes);

    String[] quarterHours = {
            "00",

            "30",

    };
    boolean isflag = false;


    times = new ArrayList<>();

    for (int i = 9; i < 22; i++) {

        if (ti > 8) {
            for (int j = 0; j < 2; …
Run Code Online (Sandbox Code Playgroud)

java datetime android date java-time

2
推荐指数
1
解决办法
9688
查看次数

如果字符串包含 000,OffsetDateTime 不会显示毫秒

我有一个字符串“2020-03-25T22:00:00.000Z”,我想将其转换为 OffsetDateTime。下面是我尝试过的代码,但是当我将毫秒作为 000 传递时,它不会反映在 OffsetDateTime 中。

OffsetDateTime offsetDateTime=OffsetDateTime.parse("2020-03-25T22:00:01.123Z", 
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
print(offsetDateTime)
//Output: 2020-03-25T22:00:01.123Z
Run Code Online (Sandbox Code Playgroud)

但是当mili为000时

OffsetDateTime offsetDateTime=OffsetDateTime.parse("2020-03-25T22:00:01.000Z", 
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
print(offsetDateTime)

//Output: 2020-03-25T22:01Z (mili second is missing)
Run Code Online (Sandbox Code Playgroud)

我也尝试了自定义格式化程序,但它的行为相同

OffsetDateTime.parse("2020-03-25T22:00:00.123Z",
           DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"));
Run Code Online (Sandbox Code Playgroud)

谁能帮我一下吗

java datetimeoffset date-formatting java-time

2
推荐指数
1
解决办法
2631
查看次数