lUj*_*kSh 2 time datetime kotlin
我是科特林新手。我遇到了一个问题。
我有这个代码:
val sdf = SimpleDateFormat("dd.MM.yyyy")
val currentDate = sdf.format(Date())
println(currentDate)
val stringDate = "12.03.2015"
val dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH)
val millisecondsSinceEpoch = LocalDate.parse(stringDate, dateFormatter)
.atStartOfDay(ZoneOffset.UTC)
.toInstant()
.toEpochMilli()
println(millisecondsSinceEpoch)
val time = currentDate - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
Run Code Online (Sandbox Code Playgroud)
但上线了:
val time = currentDate - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
Run Code Online (Sandbox Code Playgroud)
请帮助我如何解决这个问题。我需要从字符串中的日期中减去当前日期。
更新:
如何正确地从一个日期减去另一个日期并得到天数差异?
我建议您从过时的java.util日期/时间 API 切换到现代的日期/时间 API。下面给出的是满足您需求的 Java 代码,我希望您能够将其转换为 Kotlin。但是,如果您遇到任何问题,我可以为您将其转换为 Kotlin 代码。
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Define format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH);
// Given date-time
ZonedDateTime givenDateTime = LocalDateTime.of(LocalDate.parse("12.03.2015", formatter), LocalTime.of(0, 0))
.atZone(ZoneId.of("Etc/UTC"));
// Now
ZonedDateTime zdtNow = ZonedDateTime.now(ZoneId.of("Etc/UTC"));
// Period between the two dates
Period period = Period.between(givenDateTime.toLocalDate(), zdtNow.toLocalDate());
// Given date-time with current year, month and day
ZonedDateTime adjusted = givenDateTime.with(LocalDate.now(ZoneId.of("Etc/UTC")));
// Duration between the two times
Duration duration = Duration.between(adjusted, zdtNow);
// Display each part of the period and duration
System.out.printf("%d years %d month %d days %d hours %d minutes %d seconds %d nanoseconds", period.getYears(),
period.getMonths(), period.getDays(), duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart(), duration.toNanosPart());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
5 years 4 month 7 days 19 hours 30 minutes 37 seconds 507058000 nanoseconds
Run Code Online (Sandbox Code Playgroud)
OffsetDateTime:import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Define format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH);
// Given date-time
OffsetDateTime givenDateTime = LocalDateTime.of(LocalDate.parse("12.03.2015", formatter), LocalTime.of(0, 0))
.atOffset(ZoneOffset.UTC);
// Now
OffsetDateTime odtNow = OffsetDateTime.now(ZoneOffset.UTC);
// Period between the two dates
Period period = Period.between(givenDateTime.toLocalDate(), odtNow.toLocalDate());
// Given date-time with current year, month and day
OffsetDateTime adjusted = givenDateTime.with(LocalDate.now(ZoneOffset.UTC));
// Duration between the two times
Duration duration = Duration.between(adjusted, odtNow);
// Display each part of the period and duration
System.out.printf("%d years %d month %d days %d hours %d minutes %d seconds %d nanoseconds", period.getYears(),
period.getMonths(), period.getDays(), duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart(), duration.toNanosPart());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16470 次 |
| 最近记录: |