运行我的程序后,我在运行大约 2 小时后发生了奇怪的崩溃,说明它无法解析日期。
Text '2016-10-26T12:31:39.084726218Z' could not be parsed: Unable to obtain Instant from TemporalAccessor: {InstantSeconds=1477485099, NanoOfSecond=84726218},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919)
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?因为在网上查找时,我发现这可能是由于格式不正确造成的,但由于我没有指定格式,所以对我来说情况并非如此。
解析我的时间戳的代码如下:
Instant instant = Instant.parse(cAdvisor.getTimestamp());
Long epoch = instant.getEpochSecond();
Run Code Online (Sandbox Code Playgroud)
注意:该cAdvisor.getTimestamp()方法返回一个字符串,例如:'2016-10-26T12:31:39.084726218Z'
注 2:我的 java 版本报告了这一点
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b115)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b57, mixed mode)
Run Code Online (Sandbox Code Playgroud)
更新 #1:以下代码复制了此问题:
import java.time.Instant;
import java.util.Random;
// one class needs to have a main() method
public class Test
{
// arguments are …Run Code Online (Sandbox Code Playgroud) 如何按日期对折线图 X 轴进行排序?
现在我的折线图看起来像这样
我试图剪切日期并将其转换为 int,但现在我不知道该怎么办......
datesToCompare.addAll(LastHoursAndDates.keySet()); // dates in String eg. 2015-12-25
List<Integer> year = new ArrayList<Integer>(); // list for year after split
List<Integer> month = new ArrayList<Integer>();// list for month after split
List<Integer> days = new ArrayList<Integer>(); // list for days after split
for(int i =0;i < LastHoursAndDates.size();i++)
{
sorting = datesToCompare.get(i).split("-");
year.add(Integer.valueOf(sorting[0]));
month.add(Integer.valueOf(sorting[1]));
days.add(Integer.valueOf(sorting[2]));
for(int j =0;j < LastHoursAndDates.size();j++)
{
if(year.get(i) == year.get(j))
{
if(month.get(i) == month.get(j))
{
//???????
}
}
}
}//for
Run Code Online (Sandbox Code Playgroud)
也许有人有同样的问题?
我试图java.time.LocalDate在我的报告中显示以下格式:
<textField pattern="EEEEE dd MMMMM yyyy">
<reportElement x="64" y="0" width="150" height="20" uuid="5e003c53-2327-4f75-9adf-831a3bb494ff"/>
<textElement verticalAlignment="Middle">
<font isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{dateFacture}]]></textFieldExpression>
</textField>
Run Code Online (Sandbox Code Playgroud)
但我得到的日期如下:2017-03-09。是由于 java.time.LocalDate 类型造成的吗?
我想要获取例如当前时间的字符串:“ 20180122_101043”。我怎样才能做到这一点?
我可以创建一个,val cal = Calendar.getInstance()但不确定如何处理。
使用Java 1.8.0_51以下代码(取自Unable从TemporalAccessor获取OffsetDateTime)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.of("Europe/Berlin"));
OffsetDateTime offsetDateTime = ZonedDateTime.parse("20151113", formatter).toOffsetDateTime();
System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_DATE));
Run Code Online (Sandbox Code Playgroud)
抛出异常:
java.time.format.DateTimeParseException: Text '20151113' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2015-11-13 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
Run Code Online (Sandbox Code Playgroud)
这次我做错了什么?
从https://api.spacexdata.com/v3/launches获取日期 该日期的格式为:2006-03-25T10:30:00+12:00。我想将其转换为“dd,mm,yyyy”,但总是收到错误:“java.time.format.DateTimeParseException:文本'2006-03-25T10:30:00 + 12:00'无法解析,未解析的文本发现于索引 10"
我的代码:
val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)
var launchDateConverted: String= myDate.toString()
Run Code Online (Sandbox Code Playgroud)
我在字符串中获取数据,然后将其转换为日期进行格式化,然后将日期转换回字符串以在 UI 上显示。我使用了不同的方法,但找不到正确的方法。我当前的语言环境是“RU”。
最近开始使用WEB UI。并且遇到了日期字符串解析/验证的问题。“dd-mm-yyyy”我发现的一些方法是:
匹配 - 不完整的验证,不灵活。
(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])
有一个帖子,有人建议用可能的日期字符串预初始化 Set - 快速,有效,但也不灵活且消耗内存
有没有更简单的东西,也许在公共库中可用?
请不要建议 SimpleDateFormat :)
UPDATE 对Java 8正确答案是/sf/answers/3015320101/
我是 Java 新手。我想以dd/mm/yyyy格式显示日期。当我使用new Date()函数打印日期时,它给了我不想要的 Sting。假设我想打印它给我的今天的日期,Sat Jan 24 08:17:41 IST 2015但我想输出为24/1/2015
我正在使用LocalDateTime.now()方法获取文件报告的文件名的当前时间和日期,并且文件名中不允许使用冒号.
如何在字符串中用句点(".")替换每个冒号(":"),以便设置为文件名?
我想使用LocalDateTime.parse()方法使用日期格式yyyy-MM-dd HH:mm:ss解析日期,但实际得到的是格式为yyyy-MM-ddTHH:mm:ss的日期.我不需要'T'.请参阅下面的代码
LocalDateTime.parse("2016-10-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
我得到的结果是2016-10-31T23:59:59.看到'T'.'T'导致问题,因此我无法将其持久保存到我的数据库中.我尝试将值保存在类型列中datetime; 我收到了错误 - org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar.
查看有效的值
VALUES('US','101','test','firstname','middleName','preferedN??ame','lastName',
'198??9-01-01','M',1,'1122??1123','test@test.com??','address1','addres??s2','Bloomingdale','??IL','US','689850',
1,??1,'11111','2016-12-3??1 23:59:59')
Run Code Online (Sandbox Code Playgroud)
(T最后一个值没有)
哪个不起作用:
VALUES('US','101','test','firstname','middleName','preferedN??ame','lastName',
'198??9-01-01','M',1,'1122??1123','test@test.com??','address1','addres??s2','Bloomingdale','??IL','US','689850',
1,??1,'11111','2016-12-3??1T23:59:59')
Run Code Online (Sandbox Code Playgroud)
(带有T最后一个值).
我面临着一些问题解析日期格式dd-m-y与SimpleDateFormat类在Java中.
那么这个日期格式类型(12-oct-14)有任何日期格式化程序吗?
我有 DateTime 字符串 2020-04-03 01:29:27 和 2020-04-03 01:29:37 我想以小时为单位获得持续时间。我已经尝试了很多东西,但这却找不到任何帮助