Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 乔达时间加上星期四的日期弄错了_Java_Android_Datetime_Jodatime_Android Jodatime - Fatal编程技术网

Java 乔达时间加上星期四的日期弄错了

Java 乔达时间加上星期四的日期弄错了,java,android,datetime,jodatime,android-jodatime,Java,Android,Datetime,Jodatime,Android Jodatime,我正在使用以下代码: DateTime(date)//Thu Aug 06 00:00:00 GMT+03:00 2020 .plusDays(days) // 103 .toDate() 结果是12月18日星期五23:00:00 GMT+2020年02:00,而不是12月19日。 有一些日期很好,与其他结果DATE-1,我猜问题是以月为数月,但PulsDes不考虑它吗?< /P> 有些日期效果很好,另一些则是结果日期-1,我猜

我正在使用以下代码:

 DateTime(date)//Thu Aug 06 00:00:00 GMT+03:00 2020
                .plusDays(days) // 103
                .toDate()
结果是12月18日星期五23:00:00 GMT+2020年02:00,而不是12月19日。 有一些日期很好,与其他结果DATE-1,我猜问题是以月为数月,但PulsDes不考虑它吗?< /P> 有些日期效果很好,另一些则是结果日期-1,我猜 问题在于月内的天数,而冥王星不是 考虑一下?

确实考虑到了。您提到的两个日期时间字符串的问题是,它们属于不同的区域偏移。第一个是UTC+3,第二个是UTC+2。下面给出的是如何使用相同的分区偏移进行此操作

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zZ yyyy");
        DateTime dateTime = DateTime.parse("Thu Aug 06 00:00:00 GMT+03:00 2020", formatter);
        System.out.println(dateTime);
        // With Zone-Offset of UTC+2
        System.out.println(dateTime.withZone(DateTimeZone.forOffsetHours(2)));

        // Add 103 days
        DateTime dateTimeAfter103Days = dateTime.plusDays(103);
        System.out.println(dateTimeAfter103Days);
        System.out.println(dateTime.withZone(DateTimeZone.forOffsetHours(2)));
    }
}
输出:

我建议您使用java.time日期时间API和相应的格式化API包java.time.format。从了解有关现代日期时间API的更多信息。如果您的Android API级别仍然不符合Java8,请检查并重试

下表显示了一个示例:

使用现代日期时间API:

输出:

或者

输出:

有些日期效果很好,另一些则是结果日期-1,我猜 问题在于月内的天数,而冥王星不是 考虑一下?

确实考虑到了。您提到的两个日期时间字符串的问题是,它们属于不同的区域偏移。第一个是UTC+3,第二个是UTC+2。下面给出的是如何使用相同的分区偏移进行此操作

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zZ yyyy");
        DateTime dateTime = DateTime.parse("Thu Aug 06 00:00:00 GMT+03:00 2020", formatter);
        System.out.println(dateTime);
        // With Zone-Offset of UTC+2
        System.out.println(dateTime.withZone(DateTimeZone.forOffsetHours(2)));

        // Add 103 days
        DateTime dateTimeAfter103Days = dateTime.plusDays(103);
        System.out.println(dateTimeAfter103Days);
        System.out.println(dateTime.withZone(DateTimeZone.forOffsetHours(2)));
    }
}
输出:

我建议您使用java.time日期时间API和相应的格式化API包java.time.format。从了解有关现代日期时间API的更多信息。如果您的Android API级别仍然不符合Java8,请检查并重试

下表显示了一个示例:

使用现代日期时间API:

输出:

或者

输出:

您可以使用java.time,它在较低的Android API中受支持,因为现在有了

有一个区域感知类java.time.ZonedDateTime和一个偏移感知类java.time.OffsetDateTime,但是您的示例字符串只包含GMT/UTC的偏移量。这就是为什么我会使用OffsetDateTime来解析时间上的确切时刻,然后添加一天

下面是一个简单的示例,它定义了一个格式化程序,用于解析给定字符串并将其用于输出:

public static void main(String[] args) {
    // example String
    String date = "Fri Dec 18 23:00:00 GMT+02:00 2020";
    // create a formatter that is able to parse and output the String
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu",
                                                        Locale.ENGLISH);
    // parse the String using the formatter defined above
    OffsetDateTime odt = OffsetDateTime.parse(date, dtf);
    System.out.println("OffsetDateTime parsed is\t" + odt.format(dtf));
    
    // add a day to the date part
    OffsetDateTime dayLater = odt.plusDays(1);
    System.out.println("Adding a day results in\t\t" + dayLater.format(dtf));
}
这个输出

解析的OffsetDateTime为Fri Dec 18 23:00:00 GMT+02:00 2020 在Sat Dec 19 23:00:00 GMT+02:00 2020中添加一天结果 如果您对只输出日期而不输出时间部分或偏移量感兴趣,那么在这些类中还有另一件方便的事情,那就是轻松提取日期或时间部分。例如,可以使用OffsetDateTime执行以下操作:

// extract the part that only holds information about day of month, month of year and year
LocalDate dateOnly = odt.toLocalDate();
// print the default format (ISO standard)
System.out.println(dateOnly);
// or define and use a totally custom format
System.out.println(dateOnly.format(
                        DateTimeFormatter.ofPattern("EEEE, 'the' dd. 'of' MMMM uuuu",
                                                    Locale.ENGLISH)
                    )
);
这将有助于提高产量

2020-12-18 18日星期五。2020年12月31日 如果您处理的是日期选择器DatePicker,则可以按getYear、getMonth和getDayOfMonth接收选定的值,然后创建一个

LocalDate localDate = LocalDate.of(datePicker.getYear(),
                                   datePicker.getMonth(), 
                                   datePicker.getDayOfMonth());
然后只需按localDate.plusDays1添加一天

您可以使用java.time,它在较低的Android API中受支持,因为现在有了

有一个区域感知类java.time.ZonedDateTime和一个偏移感知类java.time.OffsetDateTime,但是您的示例字符串只包含GMT/UTC的偏移量。这就是为什么我会使用OffsetDateTime来解析时间上的确切时刻,然后添加一天

下面是一个简单的示例,它定义了一个格式化程序,用于解析给定字符串并将其用于输出:

public static void main(String[] args) {
    // example String
    String date = "Fri Dec 18 23:00:00 GMT+02:00 2020";
    // create a formatter that is able to parse and output the String
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu",
                                                        Locale.ENGLISH);
    // parse the String using the formatter defined above
    OffsetDateTime odt = OffsetDateTime.parse(date, dtf);
    System.out.println("OffsetDateTime parsed is\t" + odt.format(dtf));
    
    // add a day to the date part
    OffsetDateTime dayLater = odt.plusDays(1);
    System.out.println("Adding a day results in\t\t" + dayLater.format(dtf));
}
这个输出

解析的OffsetDateTime为Fri Dec 18 23:00:00 GMT+02:00 2020 在Sat Dec 19 23:00:00 GMT+02:00 2020中添加一天结果 如果您对只输出日期而不输出时间部分或偏移量感兴趣,那么在这些类中还有另一件方便的事情,那就是轻松提取日期或时间部分。例如,可以使用OffsetDateTime执行以下操作:

// extract the part that only holds information about day of month, month of year and year
LocalDate dateOnly = odt.toLocalDate();
// print the default format (ISO standard)
System.out.println(dateOnly);
// or define and use a totally custom format
System.out.println(dateOnly.format(
                        DateTimeFormatter.ofPattern("EEEE, 'the' dd. 'of' MMMM uuuu",
                                                    Locale.ENGLISH)
                    )
);
这将有助于提高产量

2020-12-18 18日星期五。2020年12月31日 如果您处理的是日期选择器DatePicker,则可以按getYear、getMonth和getDayOfMonth接收选定的值,然后创建一个

LocalDate localDate = LocalDate.of(datePicker.getYear(),
                                   datePicker.getMonth(), 
                                   datePicker.getDayOfMonth());

然后只需按localDate.plusDays1添加一天

您必须使用JodaTime还是可以使用java time java 8+?如果Android api 21+支持java time,那么我可以使用这可能是由于夏令时。时区偏移在10月25日左右从+3更改为+2。您必须使用JodaTime还是可以使用java time java 8+?如果Android api 21+支持java time,那么我可以使用这可能是由于夏令时。时区偏移量在10月25日左右从+3更改为+2。但示例字符串仅包含它实际上来自java Date对象的GMT/UTC偏移量DatePicker@PavelPoley您可以从DatePicker获取年、月和月日,只需从中创建LocalDate即可。我不再使用java.util.Date,因为像您的示例中那样的问题。查看我编辑的答案…为什么要使用localDate.plusDays1?@PavelPoley这只是一个示例,您可以输入
这里是y int。如果整数天=103;,然后执行localDate.plusDays;,没问题。但是您的示例字符串只包含一个GMT/UTC的偏移量,实际上是java日期对象的偏移量DatePicker@PavelPoley您可以从DatePicker获取年、月和月日,只需从中创建LocalDate即可。我不再使用java.util.Date,因为像您的示例中那样的问题。查看我编辑的答案…为什么要使用localDate.plusDays1?@PavelPoley这只是一个示例,您可以在那里输入任何int。如果整数天=103;,然后执行localDate.plusDays;,没问题。
LocalDate localDate = LocalDate.of(datePicker.getYear(),
                                   datePicker.getMonth(), 
                                   datePicker.getDayOfMonth());