我需要将日期字符串转换为另一种特定格式。
例如:我有一个日期可以作为YYYY-MM-DDorYYYY-MM-DDThh:mm:ss±0000并且我需要将其转换为 something "yyyy-mm-ddTHH:MM:SSZ"。
我使用 Java 8 API 尝试了一些东西,但找不到出路。
该文档YearMonth没有任何关于如何处理此问题的建议.
目前我正在使用以下方法来检查a是否LocalDate属于a YearMonth,where date和yearMonthare分别为:
YearMonth lastDayOfPreviousMonth = yearMonth.atDay(1).minusDay(1);
YearMonth firstDayOfNextMonth = yearMonth.atEndOfMonth().plusDays(1);
return date.isAfter(lastDayOfPreviousMonth)
&& date.isBefore(firstDayOfNextMonth);
Run Code Online (Sandbox Code Playgroud)
有没有更清洁或内置的方法来做到这一点?
我正在使用 Java 8,我的.txt文件中有一个字符串,我想将其转换为LocalDateTime对象。
String time1 = "2017-10-06T17:48:23.558";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd.MM.yyyy. HH:mm:ss");
LocalDateTime alarmTime = LocalDateTime.parse(time1, formatter1);
System.out.println(time1);
Run Code Online (Sandbox Code Playgroud)
这给了我这个例外:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-10-06T17:48:23.558' could not be parsed at index 2
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
PS注意这个:
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy. HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);
Run Code Online (Sandbox Code Playgroud)
在 Java 8 中不起作用。
编辑:我没有把问题说清楚,我的错:
我的.txt文件中有这个字符串,我需要将它转换为LocalDateTime对象以便将它保存到一个类对象中,但我需要按照规定的格式将它打印出来,以便在表格中打印出来。我不希望它以原始格式打印出来"2017-10-06T17:48:23.558"。我希望它打印出这样的东西:"10.06.2017. 17:48:23"
我正在尝试OffsetDateTime("2019-01-14 21:10:00.02+03")使用JDBC与PostgreSQL 一起存储时区(+03)。但是,当使用sql查询检索数据时,我总是得到+00结果。有什么方法可以在postgres中使用datetime存储偏移量(+03)吗?
我正在使用 SimpleDateFormat 来格式化或验证日期,但我想通过使用 java 8 DateTimeFormatter 使其成为线程安全的。我无法实现某些要求。
我的应用程序将只接受三种类型的格式。"yyyy-MM-dd", "yyyy-MM", "yyyy"
Existing Code gives me desired output:
/*simple date format to process yyyy-MM-dd format
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd")
/*simple date format to process yyyy-MM format
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM")
/*simple date format to process yyyy format
SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy")
/* to parse input
simpleDateFormat.parse(input)
/* to format
simpleDateFormat.format(simpleDateFormat1)
Run Code Online (Sandbox Code Playgroud)
这是输入和预期输出:
input expected
'2018-03-19' '2018-03-19'
'2018-03' '2018-03'
'2018' '2018'
'2017-02-54' '2017-02'
'2016-13-19' '2016'
Run Code Online (Sandbox Code Playgroud)
如何在 java 8 DateTimeFormat enter code …
我有一个初始时间12:00:00,我需要添加144分钟。
预期输出为15.24.00(即,初始时间增加 2.24 小时)。
我应该如何更新下面给出的当前代码?
String startTime = "13:00:00";
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
Date date1 = null;
Date date2 = null;
try {
date1 = format.parse(startTime);
} catch (ParseException e) {
e.printStackTrace();
}
Long addition = (long) (TimeUnit.MILLISECONDS.toMinutes(date1.getTime()));
System.out.println("Difference : "+addition);
Run Code Online (Sandbox Code Playgroud) 我正在尝试在java.time.LocalDate. 为什么是 LocalDate ?因为这种类型似乎最适合生日。
假设我想过滤 3 月 6 日和 4 月 30 日之间的生日,而不考虑年份。这该怎么做 ?
如果我要这样做,我会替换 LocalDate 中的年份并使用方法isAfter和isBefore. 我必须更改年份这一事实只是一个简单的技巧。或者我使用了错误的数据类型 (LocalDate)
我有这个 groovy 代码,它给了我一个错误,我不明白为什么:
import java.text.SimpleDateFormat
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters
static String convertDateTimeString(String fromFormat, String toFormat, String dateString) {
DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern(fromFormat, Locale.GERMAN)
LocalDateTime localDateTime = LocalDateTime.parse(dateString, fromFormatter)
DateTimeFormatter toFormatter = DateTimeFormatter.ofPattern(toFormat, Locale.GERMAN)
localDateTime.format(toFormatter)
}
String date = convertDateTimeString( 'EEE, dd MMM yyyy HH:mm:ss z', 'yyyy', "Wed, 04 Feb 2015 10:12:34 UTC")
assert date == '2015'
Run Code Online (Sandbox Code Playgroud)
错误是 java.time.format.DateTimeParseException: Text 'Wed, 04 Feb 2015 10:12:34 UTC' could not be parsed at index 0 …
将月份添加到 LocalDate 时,我遇到了奇怪的格式问题。这是 Scala 代码和输出:
val virtualToday: LocalDate = LocalDate.parse("2015-01-01")
val eightDaysFromToday: LocalDate = virtualToday.plusDays(8)
val sixMonthsFromToday: LocalDate = virtualToday.plusMonths(6)
println("virtualToday " + virtualToday)
println("eightDaysFromToday " + eightDaysFromToday)
println("sixMonthsFromToday " + sixMonthsFromToday)
println(
"virtualToday with formatting " + virtualToday
.format(DateTimeFormatter.ofPattern("D MMMM Y"))
)
println(
"eightDaysFromToday with formatting " + eightDaysFromToday
.format(DateTimeFormatter.ofPattern("D MMMM Y"))
)
println(
"sixMonthsFromToday with formatting " + sixMonthsFromToday
.format(DateTimeFormatter.ofPattern("D MMMM Y"))
)
Run Code Online (Sandbox Code Playgroud)
这是输出:
虚拟
今天 2015-01-01
八天从今天 2015-01-09六个月从今天 2015-07-01
virtualToday 格式为 2015 年 1 月 1 日 …
我有以下格式的列表,我想将此列表分组为分钟间隔。
List<Item> myObjList = Arrays.asList(
new Item(LocalDateTime.parse("2020-09-22T00:13:36")),
new Item(LocalDateTime.parse("2020-09-22T00:17:20")),
new Item(LocalDateTime.parse("2020-09-22T01:25:20")),
new Item(LocalDateTime.parse("2020-09-18T00:17:20")),
new Item(LocalDateTime.parse("2020-09-19T00:17:20")));
Run Code Online (Sandbox Code Playgroud)
例如,给定 10 分钟的间隔,列表的前 2 个对象应该在同一个组中,第三个应该在不同的组中,依此类推。
可以使用 Java 的 8groupingBy函数将此 List 分组为间隔吗?
我的解决方案是将列表中的每个日期与列表中的所有其他日期进行比较,并在新列表中添加不同 X 分钟的日期。这似乎是非常缓慢和“hacky”的解决方法,我想知道是否有更稳定的解决方案。