首先,我是新手java.time包装.
我正在编写一个webapp,需要在一天中的特定时间和几个事件持续时间内工作.
所以我用包LocalTime和Duration类编写了我的代码java.time.
当我需要在JSP中呈现它们的值时,它对于LocalTime对象来说非常简单(因为.toString()返回一个人类可读的变量),所以我可以只是编写${startTime}并且所有内容都以正确的方式进行(例如,它呈现为9:00).相同的方法对于持续时间不起作用,因为它的表示类似于PT20M(在这种情况下为20分钟).
是否存在一种优雅的方式来直接通过EL在JSP中执行人类可读的转换?
是的,我知道我可以在我的类中(在JSP之前)转换字符串中的对象,但我正在寻找建议的方法(我无法找到)...另一点是我看不到在Duration对象中官方的"convert()"(或其他任何)方法...所以我在想我正在使用错误的对象来映射持续时间(在LocalTimes 上加或减).
谢谢.
有没有办法在 Kotlin 或 Java 中格式化本地化的持续时间?我有一个持续时间,我想以本地化的、人类可读的字符串显示它,例如“1 分钟 30 秒”或“1m 30s”。
我确实找到了RelativeDateTimeFormatter,但它似乎只生成将来时或过去时的时间部分,例如“1分钟前”或“1分钟内”。有没有办法配置它来生成现在时?
LocalDateTime start = LocalDateTime.of(2018, 9, 2, 14, 17, 25, 135);
LocalDateTime end = LocalDateTime.of(2018, 9, 2, 14, 19, 59, 965);
Duration duration = Duration.between(start, end);
Run Code Online (Sandbox Code Playgroud)
我需要获得这种格式的结果:2:34.830。
分钟和纳秒 - 没问题:
System.out.println(duration.toMinutes());
System.out.println(duration.toNanos());
Run Code Online (Sandbox Code Playgroud)
但我怎样才能从这个例子中获得秒数 - 34 秒?
我正在尝试计算考勤应用程序的持续时间列表的总和。我能够计算出 2 次的差异,但不确定如何遍历列表以将日期相加。
下面的代码是我到目前为止所尝试的。列表中有 2 个持续时间,它们使用对象类从 Firebase 添加到列表中。
持续时间 1 = 0:5:42
持续时间 2 = 0:0:09
预期总数 = 0:5:51
//初始化
long sum;
Run Code Online (Sandbox Code Playgroud)
//当前方法尝试
public void grandTotal() throws ParseException {
java.util.Date date1;
java.util.Date date2;
DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
for(int i = 0 ; i < newList.size(); i++) {
String str_time1 = newList.get(i).getDuration();
date1 = formatter.parse(str_time1);
for (int j = 1; j < newList.size(); j++) {
String str_time2 = newList.get(j).getDuration();
date2 = formatter.parse(str_time2);
sum = date1.getTime() + date2.getTime();
}
}
secs …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算两个小时之间的差异。时间格式必须是 hh:mm:ss!我实现了这个代码:
public static String timeDifference(long timeDifference1) {
long timeDifference = timeDifference1 / 1000;
int h = (int) (timeDifference / (3600));
int m = (int) ((timeDifference - (h * 3600)) / 60);
int s = (int) (timeDifference - (h * 3600) - m * 60);
return String.format("%02d:%02d:%02d", h, m, s);
}
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String timeStart = sc.next();
String timeStop = sc.next();
char lol[]=timeStop.toCharArray();
if(lol[0]=='0' && lol[1]=='0'){ …Run Code Online (Sandbox Code Playgroud) 我尝试了这段代码:
public class TimePassed {
private long seconds;
private long minutes;
private long hours;
private long days;
private long years;
...
public TimePassed(double unixSeconds) {
Instant now = Instant.now();
Instant ago = Instant.ofEpochSecond((long) unixSeconds);
this.seconds = ChronoUnit.SECONDS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //6100
this.minutes = ChronoUnit.MINUTES.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //101
this.hours = ChronoUnit.HOURS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //1
this.days = ChronoUnit.DAYS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //0
this.years = ChronoUnit.YEARS.between(
ago.atZone(ZoneId.systemDefault()),
now.atZone(ZoneId.systemDefault())); //0
}
}
Run Code Online (Sandbox Code Playgroud)
然而,那么该TimePassed对象将具有seconds = 6100和,而我希望它是minutes = 101, ,所以。可以做包吗?因为到目前为止,我只能得到过去的秒数、过去的分钟或过去的小时等等。而且两者都无法解释对方。hours = …
有没有办法缩短这段代码?在代码中,微秒时间被转换为分钟和秒。例如 305862000 毫秒 = 05:05(5 分钟:5 秒)。
public static String timeFormatter (long microtime) {
String time = null, timemin = null, timesec = null;
long min = 0;
long sec = 0;
// check if time is negative
if (microtime < 0) {
throw new RuntimeException("Negativ time value provided");
} else {
min = (long) (microtime/(Math.pow((double) 10,(double) 6))/60);
if (min < 10) {
timemin = "0" + min;
// check if time is too long
} else if (min > …Run Code Online (Sandbox Code Playgroud) 如果我能按照我的要求做,那就完美了!
<<LocalTime is provided as locTime>>
Date flyDate = a.getDate();
Date landingDate = b.getDate();
flyDate.add(locTime);
Run Code Online (Sandbox Code Playgroud)
或者我可以了解什么是最佳实践。
我有一堆 Flight 对象,目前它们每个都有一个 Date 来确定航班的飞行日期/时间,还有一个用于 LocalTime 的持续时间,表示飞行需要多少小时:分钟。
我想检查一个航班在时间方面是否与另一个航班兼容。航班a的着陆时间应该在航班b的飞行时间之前。
谢谢!