如下所示,
LocalDateTime currentUTCTime = LocalDateTime.now(ZoneId.of("UTC"));
String reqPattern = currentUTCTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
System.out.println("Required pattern: " + reqPattern);
GregorianCalendar calendar = GregorianCalendar.from(currentUTCTime.atZone(ZoneId.systemDefault()));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
System.out.println("But Showing As :" + xcal);
Run Code Online (Sandbox Code Playgroud)
我希望输出为2015-06-18 11:59:15:135,但是当我设置xcal为XML标记时XMLGregorianCalender,它显示为2015-06-18T11:59:15.135+05:30.
我该如何删除该+05:30部分?
我开发了一个SonarQube插件,为了满足我的一个需求,我需要将项目的分析日期存储为SQL TIMESTAMP(请注意: a TIMESTAMP,而不是 a TIMESTAMP WITH TIMEZONE).
以下是我目前的工作方式:
// In the SonarQube Sensor
// .getAnalysisDate() returns a java.util.Date
final Instant instant = module.getAnalysisDate().toInstant();
// Timestamp at UTC from the Instant
final LocalDateTime dt = LocalDateTime.frominstant(instant, ZoneOffset.UTC);
final Timestampt ts = Timestamp.valueOf(dt);
Run Code Online (Sandbox Code Playgroud)
抓住Instant的概念我有点麻烦,还有ZonedDateTime等......
无论如何,这似乎做我想要的,但这是正确的方法吗?
我在运行一些代码时遇到了一个奇怪的错误.这是一个展示相同的简单版本.
public class DateTest {
public static void main(String[] args) {
LocalDate decLast = LocalDate.of(2015, 12, 31);
LocalDate novLast = LocalDate.of(2015, 11, 30);
LocalDate octLast = LocalDate.of(2015, 10, 31);
System.out.println(decLast+" "+novLast+" "+octLast);
System.out.println(decLast.format(DateTimeFormatter.ofPattern("dd M YY"))+" "
+novLast.format(DateTimeFormatter.ofPattern("dd M YY"))+" "
+octLast.format(DateTimeFormatter.ofPattern("dd M YY")));
}
}
Run Code Online (Sandbox Code Playgroud)
这返回了以下结果
2015-12-31 2015-11-30 2015-10-31
31/12/16 30/11/15 31/10/15
Run Code Online (Sandbox Code Playgroud)
不知何故,2015年12月31日被转换为2016年12月31日.我写了一个for循环来做同样的不同年份,并发现多年来存在变化.12月26日以下的任何日期都不存在该错误.这是一个错误还是我错过了什么?
使用Joda时间解析借记卡/信用卡的到期日期非常容易:
org.joda.time.format.DateTimeFormatter dateTimeFormatter = org.joda.time.format.DateTimeFormat.forPattern("MMyy").withZone(DateTimeZone.forID("UTC"));
org.joda.time.DateTime jodaDateTime = dateTimeFormatter.parseDateTime("0216");
System.out.println(jodaDateTime);
Run Code Online (Sandbox Code Playgroud)
日期: 2016-02-01T00:00:00.000Z
我尝试使用Java Time API做同样的事情:
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("MMyy").withZone(ZoneId.of("UTC"));
java.time.LocalDate localDate = java.time.LocalDate.parse("0216", formatter);
System.out.println(localDate);
Run Code Online (Sandbox Code Playgroud)
输出:
引起:java.time.DateTimeException:无法从TemporalAccessor获取LocalDate:{MonthOfYear = 2,Year = 2016},ISO,UTC,类型为java.time.format.Parsed,类型为java.time.LocalDate.from(LocalDate.java :368)java.time.format.Parsed.query(Parsed.java:226)at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)... 30 more
我犯了什么错误以及如何解决它?
我想从以下模式解析LocalDateTime:
yyyyMMddHHmmss000000
Run Code Online (Sandbox Code Playgroud)
这意味着通常的"yyyy ... ss"然后是六个尾随的零.
因此,格式化工作正常:
String p = "yyyyMMddHHmmss'000000'";
LocalDateTime.now().format(DateTimeFormatter.ofPattern(p));
Run Code Online (Sandbox Code Playgroud)
然后解析:
String p, v;
p = "yyyyMMddHHmmss"; // without '000000'
v = "20160131235930";
LocalDateTime.parse(v, DateTimeFormatter.ofPattern(p)); // it works
p = "yyyy-MMddHHmmss'000000'"; // with '-' in between
v = "2016-0131235930000000";
LocalDateTime.parse(v, DateTimeFormatter.ofPattern(p)); // it works
p = "yyyyMMddHHmmss'000000'"; // with '000000' but without '-'
v = "20160131235930000000";
LocalDateTime.parse(v, DateTimeFormatter.ofPattern(p)); // it throws Exception
Run Code Online (Sandbox Code Playgroud)
例外情况:
java.time.format.DateTimeParseException: Text '20160131235930000000' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.LocalDateTime.parse(LocalDateTime.java:492) …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析dd-MMM-yyyy格式的日期.
package com.company;
import javax.swing.text.DateFormatter;
import java.time.format.DateTimeFormatter;
import java.time.*;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// write your code here
MonthDay m;
Locale.setDefault(Locale.ENGLISH);
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String dateString = "12-jan-1900";
try
{
LocalDate ddd = LocalDate.parse(dateString,dTF);
System.out.println(ddd.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
//System.out.println(d.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
它抛出以下异常
java.time.format.DateTimeParseException: Text '12-jan-1900' could not be parsed at index 3
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at com.company.Main.main(Main.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) …Run Code Online (Sandbox Code Playgroud) 如果我在这里混淆任何ISO日期相关条款,请提前道歉.
我希望能够在给定的一年中每周进行一次迭代(比如,2015年).我意识到您可以计算2015年1月1日到2015年12月31日之间的周数,但这不符合一周的ISO标准.相反,它给出了两个日期之间7天的时间段.2015年第一个ISO周并不一定从2015年1月1日开始.
如果我可以获得第一周的第一个日期,我相信我可以简单地迭代ZonedDateTime.plusWeeks(1)52周.您可以通过字段访问者获取任意日期的周数:
ZonedDateTime date = ZonedDateTime.now();
WeekFields weekFields = WeekFields.of(Locale.getDefault());
int weekNumber = date.get(weekFields.weekOfWeekBasedYear());
Run Code Online (Sandbox Code Playgroud)
鉴于此,我认为必须有可能在Java8 Time API中获取特定年份第一周第一天的日期,但我还没有找到办法.
我正在为java.util.Date和java.time.LocalDateTime之间的java-8转换编写一些测试代码,发现异常似乎发生在从正常时间到夏季的转换后的一小时内,当年是2038年或更高.
我只是想知道这是jdk8中的错误,还是我做错了什么?
注意:我使用的是Windows-7,64位jdk,所以不应该受到2038-unix bug的影响,这会产生更糟糕的影响.
这是我的演示代码:
package conversiontest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
public class ConversionTest {
public static void main(String[] args) {
new ConversionTest().testDateConversion();
}
// Method under test:
public java.util.Date toJavaUtilDate(LocalDateTime localDateTime) {
return java.util.Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
// Test-code:
public void testDateConversion() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LocalDate localDate = LocalDate.of(2016, 1, 1);
LocalTime localTime = LocalTime.of(3, 22, 22); // 03:22:22
while …Run Code Online (Sandbox Code Playgroud) 假设我希望我的星期在星期二开始,那天应该从早上5:30开始.
这意味着,像这样的代码应该工作:
// LocalDateTimes created with the "standard" ISO time
LocalDateTime tuesday_4_30 = LocalDateTime.now()
.with(TemporalAdjusters.next(DayOfWeek.TUESDAY))
.withHour(4).withMinute(30);
LocalDateTime tuesday_6_30 = tuesday_4_30.withHour(6).withMinute(30);
LocalDateTime previous_monday = tuesday_4_30.minusDays(1);
// eventual adjustment using TemporalAdjusters here? like this?
// tuesday_4_30 = tuesday_4_30.with(new MyTemporalAdjuster(DayOfWeek.TUESDAY, 5, 30));
// <do the same for 6_30 and previous monday>
// or possibly change some global parameter like Chronology, Locale, or such..
Assert.assertEquals(tuesday_4_30.getDayOfWeek(), DayOfWeek.MONDAY);
Assert.assertEquals(tuesday_6_30.getDayOfWeek(), DayOfWeek.TUESDAY);
// there is 1 week between the previous monday and the next tuesday 6:30
Assert.assertEquals( ChronoUnit.WEEKS.between(previous_monday,tuesday_6_30), …Run Code Online (Sandbox Code Playgroud) 在下面的剪辑中,属性$F是类java.time.LocalDateTime或java.time.LocalDate.
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[$F{theLocalDateTime}]]></textFieldExpression>
</textField>
Run Code Online (Sandbox Code Playgroud)
如何pattern在jasper报告中使用textField格式化此属性 ?