英文:
Conversion from time Duration to time String
问题
我通过JSON以这种格式获取时间 "DocTime": "PT18H30M"
。
如何将其从这种格式转换为正常的时间字符串,例如 "6:30 pm"
在Android中?
暂时我找到了这个解决方案:
String json = "PT18H30M";
System.out.println("Server Time: " +json);
int h = json.indexOf("H");
int m = json.indexOf("M");
int hrs = Integer.valueOf(json.substring(2 , h));
// System.out.println("hrs: " + hrs);
int min = Integer.valueOf(json.substring((h+1) , m));
// System.out.println("min: " + min);
String shrs = (hrs>12)? String.valueOf((hrs - 12)) : String.valueOf(hrs);
String mode = (hrs>12)? "pm" : "am";
String fTime = shrs+":"+min+" "+mode;
System.out.println("Normal Time: " +fTime);
英文:
I am getting time through JSON in this format "DocTime": "PT18H30M"
.
How to convert from this format to normal String time e.g. "6:30 pm"
in android?
For time being I found this Solution:
String json = "PT18H30M";
System.out.println("Server Time: " +json);
int h = json.indexOf("H");
int m = json.indexOf("M");
int hrs = Integer.valueOf(json.substring(2 , h));
// System.out.println("hrs: " + hrs);
int min = Integer.valueOf(json.substring((h+1) , m));
// System.out.println("min: " + min);
String shrs = (hrs>12)? String.valueOf((hrs - 12)) : String.valueOf(hrs);
String mode = (hrs>12)? "pm" : "am";
String fTime = shrs+":"+min+" "+mode;
System.out.println("Normal Time: " +fTime);
答案1
得分: 2
**使用 Java-8:**
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
Duration duration = Duration.parse("PT18H30M");
LocalTime time = LocalTime.of((int) duration.toHours(), (int) (duration.toMinutes() % 60));
System.out.println(time.format(DateTimeFormatter.ofPattern("h:m a")));
}
}
**输出:**
6:30 pm
如果您的 Android API 级别仍不符合 Java-8,请查看[通过 desugaring 可用的 Java 8+ API](https://developer.android.com/studio/write/java8-support-table)和[如何在 Android 项目中使用 ThreeTenABP](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project)。
**使用 Java-9:**
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
Duration duration = Duration.parse("PT18H30M");
LocalTime time = LocalTime.of(duration.toHoursPart(), duration.toMinutesPart());
System.out.println(time.format(DateTimeFormatter.ofPattern("h:m a")));
}
}
**输出:**
6:30 pm
请注意,[`Duration#toHoursPart`][1] 和 [`Duration#toMinutesPart`][2] 是在 Java-9 中引入的。
[1]: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#toHoursPart--
[2]: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#toMinutesPart--
英文:
Do it as follows:
With Java-8:
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
Duration duration = Duration.parse("PT18H30M");
LocalTime time = LocalTime.of((int) duration.toHours(), (int) (duration.toMinutes() % 60));
System.out.println(time.format(DateTimeFormatter.ofPattern("h:m a")));
}
}
Output:
6:30 pm
If your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
With Java-9:
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
Duration duration = Duration.parse("PT18H30M");
LocalTime time = LocalTime.of(duration.toHoursPart(), duration.toMinutesPart());
System.out.println(time.format(DateTimeFormatter.ofPattern("h:m a")));
}
}
Output:
6:30 pm
Note that Duration#toHoursPart
and Duration#toMinutesPart
were introduced with Java-9.
答案2
得分: 2
## java.time通过解糖或ThreeTenABP实现,处理时间
看起来您收到了一个字符串,其中有人误解了ISO 8601标准,并给您提供了一个表示持续时间的字符串,而他们本意是要表示一天中的某个时间。
我建议您使用java.time,即现代Java日期和时间API,来处理时间。可以通过将不正确的字符串视为从00:00开始的持续时间来解决问题。
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
String docTimeString = "PT18H30M";
Duration docTimeDur = Duration.parse(docTimeString);
LocalTime docTimeOfDay = LocalTime.MIDNIGHT.plus(docTimeDur);
String humanReadableDocTime = docTimeOfDay.format(formatter);
System.out.println(humanReadableDocTime);
输出结果:
> 6:30 PM
我利用了 `Duration.parse()` 预期并解析ISO 8601格式的特点。
## "调用需要API级别26(当前最低级别为16):java.time.Duration#parse"显示此内容
java.time在较旧和较新的Android设备上都能很好地工作。它只需要至少 **Java 6**。
- 在Java 8及更高版本以及较新的Android设备(从API级别26开始),现代API已内置。
- 在非Android环境的Java 6和7中,可以使用ThreeTen Backport,即现代类的后移版本(ThreeTen for JSR 310)。请参阅底部的链接。
- 在较旧的Android上,可以使用解糖或ThreeTen Backport的Android版本,称为ThreeTenABP。在后一种情况下,确保从`org.threeten.bp`及其子包中导入日期和时间类。
## 链接
- [Oracle教程:日期和时间](https://docs.oracle.com/javase/tutorial/datetime/),解释了如何使用java.time。
- [Java规范请求(JSR)310](https://jcp.org/en/jsr/detail?id=310),首次描述了`java.time`。
- [ThreeTen Backport项目](http://www.threeten.org/threetenbp/),将`java.time`后移至Java 6和7(ThreeTen for JSR-310)。
- [通过解糖可用的Java 8+ API](https://developer.android.com/studio/write/java8-support-table)
- [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP),ThreeTen Backport的Android版本
- [问题:如何在Android项目中使用ThreeTenABP](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project),提供了非常详细的解释。
- [维基百科文章:ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)
英文:
java.time either through desugaring or through ThreeTenABP
It seems that you have received a string where someone has misunderstood the ISO 8601 standard and given you a string representing a duration where they intended a time of day.
I recommened you use java.time, the modern Java date and time API, for your time work. The problem with the incorrect string can be solved by regarding it as a duration since 00:00.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
String docTimeString = "PT18H30M";
Duration docTimeDur = Duration.parse(docTimeString);
LocalTime docTimeOfDay = LocalTime.MIDNIGHT.plus(docTimeDur);
String humanReadableDocTime = docTimeOfDay.format(formatter);
System.out.println(humanReadableDocTime);
Output:
> 6:30 PM
I am exploiting the fact that Duration.parse()
expects and parses ISO 8601 format.
"Call requires API level 26 (current min is 16): java.time.Duration#parse" showing this
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Wikipedia article: ISO 8601
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论