我想将a转换ZonedDateTime为a String格式("dd/MM/yyyy - hh:mm").我知道这在Joda-Time其他类型中是可能的,只是使用它们toString("dd/MM/yyyy - hh:mm")....但这不起作用ZonedDateTime.toString().
ZonedDateTime为String?编辑:
我试图在另一个时区打印时间,结果似乎总是一样:
ZonedDateTime date = ZonedDateTime.now();
ZoneId la = ZoneId.of("America/Los_Angeles");
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la);
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date));
// same result as the previous one
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date2));
Run Code Online (Sandbox Code Playgroud)
而且我和洛杉矶不在同一时区.
编辑2:
找到了如何更改时区:
// Change this:
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la); // incorrect!
// To this:
ZonedDateTime date2 = date.withZoneSameInstant(la);
Run Code Online (Sandbox Code Playgroud) ISO 8601定义了表示时间间隔的语法.
表达时间间隔有四种方式:
如果结束值中缺少任何元素,则假定它们与包括时区的起始值相同.标准的这一特征允许简洁地表示时间间隔.例如,包括开始和结束时间在内的两小时会议的日期可以简单地显示为"2007-12-14T13:30/15:30",其中"/ 15:30"表示"/ 2007-12-" 14T15:30"(与开始时间相同),或每月结算期间的开始和结束日期为"2008-02-15/03-14",其中"/ 03-14"表示"/ 2008-03 -14"(与开始的同一年).
另外,通过在间隔表达式的开头添加"R [n] /"来形成重复间隔,其中R用作字母本身,[n]由重复次数代替.省略[n]的值意味着无限次重复.因此,要从"2008-03-01T13:00:00Z"开始重复"P1Y2M10DT2H30M"的间隔五次,请使用"R5/2008-03-01T13:00:00Z/P1Y2M10DT2H30M".
我正在寻找一个好的Java解析器(如果可能与Joda-Time库兼容)来解析这个语法.任何指向好图书馆的指针?
我尝试过如下,但在两种情况下它都显示同一时间?我做错了什么.
LocalDateTime currentTime = LocalDateTime.now(ZoneId.of("UTC"));
Instant instant = currentTime.toInstant(ZoneOffset.UTC);
Date currentDate = Date.from(instant);
System.out.println("Current Date = " + currentDate);
currentTime.plusHours(12);
Instant instant2 = currentTime.toInstant(ZoneOffset.UTC);
Date expiryDate = Date.from(instant2);
System.out.println("After 12 Hours = " + expiryDate);
Run Code Online (Sandbox Code Playgroud)
"当前日期"时间与"12小时后"相同...
这些是包中的两个字段java.time.temporal:
WeekFields.ISO.weekBasedYear()
ISO-8601定义了除其他两种日期之外的所谓周日期,即通常的日历日期(包括年,月和日)和顺序日期(包括年和日) ).星期日的格式为YYYY-'W'ww-e.w代表一周的年份,e代表数字ISO-星期几.Y代表以周为基础的年份,与日历年相同,但在日历年的开始或结束时除外,因为基于周的年份与最终可能在上一年开始的周周期相关联.有两条规则对于理解星期日的形成非常重要:
乍一看,两个JSR-310字段看起来都是相同的,因为ISO-8601只提到了一种基于周的年份.但是等一下,惊喜.让我们考虑以下代码示例:
LocalDate date1 =
LocalDate.of(2000, 2, 29).with(IsoFields.WEEK_BASED_YEAR, 2014);
System.out.println("IsoFields-Test: " + date1); // output: 2014-03-01
LocalDate date2 =
LocalDate.of(2000, 2, 29).with(WeekFields.ISO.weekBasedYear(), 2014);
System.out.println("WeekFields-Test: " + date2); // output: 2014-02-25
Run Code Online (Sandbox Code Playgroud)
虽然我非常了解第二种变化,但我很惊讶看到第一次约会的不同结果是在其类名中使用"官方"ISO-8601-reference.要解释计算结果:
2000-02-29日期对应于ISO-weekdate-notation中的2000-W09-2,而2014-02-25对应于2014-W09-2,保留了一周中的星期和星期几.到目前为止很好.较小字段的这种保留特性类似于如何更改日历年的规则(在大多数情况下应该保持日历日期中的月份和日期不变).
但结果2014-03-01是什么?这里算法简单地将相应的周日添加了四天,以便考虑字段"日期"(29对25)的差异.我没有找到此行为的任何来源或官方文档.有谁知道我们在哪里可以找到这两个领域之间差异的理由?有关算法行为的任何文档?
更新:
现在,我尝试使用此表达式测试新API的自我一致性,以便找出更好地支持哪两个字段:
System.out.println(
"14 week-based-years later = "
+ LocalDate.of(2000, 2, 29).plus(14, IsoFields.WEEK_BASED_YEARS));
Run Code Online (Sandbox Code Playgroud)
输出是2014-03-01类似于描述的情况IsoFields.WEEK_BASED_YEAR,尽管我仍然发现2014-02-25(= 2014-W09-2)的结果更合乎逻辑.由于此类时间单元也在类中找到,IsoFields因此行为在类中是自洽的IsoFields.看起来像一个没有文档和非直观的"功能".
我使用的版本是:java.runtime.version = 1.8.0-b132
更多测试:
LocalDate d = LocalDate.of(2014, 3, 1); // 2014-W09-6
System.out.println( …Run Code Online (Sandbox Code Playgroud) 以下代码不打印"CE"或"当前时代":
System.out.println(IsoEra.CE.getDisplayName(TextStyle.SHORT, Locale.UK)); // output: AD
System.out.println(IsoEra.CE.getDisplayName(TextStyle.FULL, Locale.UK)); // output: Anno Domini
Run Code Online (Sandbox Code Playgroud)
当然,IsoEra.CE.name()如果需要像"普通时代"或"当代时代"这样的完整显示名称,则有所帮助.我认为这有点奇怪,因为javadoc IsoEra在其类描述中明确提到术语"当前时代".它甚至不适用于根区域设置.这里的用例是为非宗教背景的客户提供服务.
这也没有帮助:
LocalDate date = LocalDate.now();
String year = date.format(DateTimeFormatter.ofPattern("G yyyy", Locale.UK)); // AD 2015
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
我找到的唯一方法是:
TextStyle style = ...;
Map<Long,String> eras = new HashMap<>();
long bce = (long) IsoEra.BCE.getValue(); // 0L
long ce = (long) IsoEra.CE.getValue(); // 1L
if (style == TextStyle.FULL) {
eras.put(bce, "Before current era");
eras.put(ce, "Current era");
} else {
eras.put(bce, "BCE");
eras.put(ce, "CE");
}
DateTimeFormatter dtf =
new …Run Code Online (Sandbox Code Playgroud) 我目前在理解 WordPress 的元查询时遇到了一个小问题。初始情况:
具有 2 个元字段(offer_start-date、offer_end-date)的自定义帖子类型 CPT 旨在作为要约,应在指定的时间段内(开始日期和结束日期之间)显示。此处的日期格式为德语格式 DD.MM.YYYY。为此,我目前使用以下查询:
$args = array(
'post_type' => 'offer',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'offer_start-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '<='
),
array(
'key' => 'offer_end-date',
'value' => date( 'd.m.Y', time() ),
'type' => 'numeric',
'compare' => '>='
)
)
);
new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)
不幸的是,查询不会产生可靠的结果。我什至不能说 100% 为什么。在某些日子里,所有优惠都会出现,而在其他日子里,则没有任何优惠。
我也曾尝试在Codex中找出问题的原因,但似乎我是一个强大的笨蛋。
这是我约会的日期"15-05-2014 00:00:00"
如何将IST转换为UTC即(至14-05-2014 18:30:00)
我的代码是
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("IST")); //here set timezone
System.out.println(formatter.format(date));
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); //static UTC timezone
System.out.println(formatter.format(date));
String str = formatter.format(date);
Date date1 = formatter.parse(str);
System.out.println(date1.toString());
Run Code Online (Sandbox Code Playgroud)
如果用户从任何区域输入相同的日期,那么将获得UTC时间(例如:从澳大利亚然后15-05-2014 00:00:00到14-05-2014 16:00:00)
请任何建议.
我运行模拟器时收到此消息,但应用程序运行良好.请帮我解决一下.
[2013-03-12 12:58:08 - tes] ------------------------------
[2013-03-12 12:58:08 - tes] Android Launch!
[2013-03-12 12:58:08 - tes] adb is running normally.
[2013-03-12 12:58:08 - tes] Performing com.example.tes.MainActivity activity launch
[2013-03-12 12:58:08 - tes] Automatic Target Mode: launching new emulator with compatible AVD 'andro-google8'
[2013-03-12 12:58:08 - tes] Launching a new emulator with Virtual Device 'andro-google8'
[2013-03-12 12:58:12 - Emulator] Failed to create Context 0x3005
[2013-03-12 12:58:12 - Emulator] emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
[2013-03-12 12:58:12 - …Run Code Online (Sandbox Code Playgroud) 当前的 Apache Avro (1.8.2) 文档提到了“持续时间”逻辑类型:
持续时间逻辑类型注释大小为 12 的 Avro 固定类型,它存储三个小端无符号整数,代表不同时间粒度的持续时间。第一个以月为单位存储一个数字,第二个以天为单位存储一个数字,第三个以毫秒为单位存储一个数字。
虽然这一切都说得通,但我在 .Net 或 Java 库中都找不到实际的实现。逻辑类型的文档清楚地列出了除持续时间(日期、时间毫秒、时间微秒、时间戳毫秒和时间戳微秒)之外的所有逻辑类型。
“持续时间”在我的 Avro 架构中相应定义:
{
"type": "record",
"name": "DataBlock",
"fields": [
{
"name": "duration",
"type": {
"type": "fixed",
"name": "DataBlockDuration",
"size": 12
}
}]
}
Run Code Online (Sandbox Code Playgroud)
在.Net(请原谅VB)中,我必须手动序列化持续时间:
Dim ret(11) As Byte
Dim months = BitConverter.GetBytes(duration.Months)
Dim days = BitConverter.GetBytes(duration.Days)
Dim milliseconds = BitConverter.GetBytes(duration.Milliseconds)
Array.Copy(months, 0, ret, 0, 4)
Array.Copy(days, 0, ret, 4, 4)
Array.Copy(milliseconds, 0, ret, 8, 4)
Run Code Online (Sandbox Code Playgroud)
在 Java 中反序列化时,我必须通过执行以下操作转换为 …
我需要将时间间隔表示为这样的本地化字符串:10 hours 25 minutes 1 second取决于Locale。用英语手工实现非常容易:
String hourStr = hours == 1 ? "hour" : "hours"等等。
但是我需要根据不同语言的规则提供一些“开箱即用”的Java(也许是Java8)机制。Java是否具有它,或者我需要针对我自己在应用程序中使用的每个语言环境实现它?