如何将日期格式从2020-09-28T11:47:37.217转换为只有11:47?

huangapple go评论135阅读模式
英文:

how to convert the date format: 2020-09-28T11:47:37.217 to just 11:47?

问题

  1. 通过 API 调用我收到以下日期/时间格式:

"AcceptedDate": "2020-09-28T11:47:37.217",
"Pickup1ArrivedDate": "2020-10-06T17:28:12.6",
"Pickup1LoadedDate": "2020-10-06T17:57:54.84",
"Pickup1DepartedDate": "2020-10-06T18:18:59.927"

  1. 是否有办法在将响应保存在 Java/Android Studio 中后,以 "11:47" 格式显示时间?
  2. 任何帮助将不胜感激。谢谢 :-)
英文:

I receive the following date/time format through api call:

  1. "AcceptedDate": "2020-09-28T11:47:37.217",
  2. "Pickup1ArrivedDate": "2020-10-06T17:28:12.6",
  3. "Pickup1LoadedDate": "2020-10-06T17:57:54.84",
  4. "Pickup1DepartedDate": "2020-10-06T18:18:59.927"

Is there anyway to show the time in the format"11:47" after saving the response in java/android studio.
Any help is appreciated. Thanks 如何将日期格式从2020-09-28T11:47:37.217转换为只有11:47?

答案1

得分: 1

你可以使用 substring() 方法来创建一个实现此功能的方法:

  1. public String stripRedundantDate(String date){
  2. return date.substring(11, 16);
  3. }

以下是一个程序示例:

  1. public class Main{
  2. public static void main(String []args){
  3. String time = "2020-09-28T11:47:37.217";
  4. System.out.println(stripRedundantDate(time));
  5. }
  6. public static String stripRedundantDate(String date){
  7. return date.substring(11, 16);
  8. }
  9. }

产生的结果为:

  1. 11:47

如果你的日期长度不固定

使用以下方法:

  1. public static String stripRedundantDate(String date){
  2. return date.substring(date.indexOf(':')-2, date.indexOf(':')+3);
  3. }
英文:

You can use substring() to create a method to do so:

  1. public String stripRedundantDate(String date){
  2. return date.substring(11, 16);
  3. }

The following program:

  1. public class Main{
  2. public static void main(String []args){
  3. String time = "2020-09-28T11:47:37.217";
  4. System.out.println(stripRedundantDate(time));
  5. }
  6. public static String stripRedundantDate(String date){
  7. return date.substring(11, 16);
  8. }
  9. }

Produces the result:

  1. 11:47

If your date is not a fixed length

Use the following:

  1. public static String stripRedundantDate(String date){
  2. return date.substring(date.indexOf(':')-2, date.indexOf(':')+3);
  3. }

答案2

得分: 0

我会将其转换为日期对象,然后以这种方式处理:

  1. LocalDate date = LocalDate.parse(apiResult.get("AcceptedDate"));

然后,我会将其格式化为您想要的外观:

  1. SimpleDateFormat format = new SimpleDateFormat("HH:mm");
  2. String dateString = format.format(date); // 这个字符串将是您要查找的时间
英文:

I would convert it to a date object and then work with it that way

  1. LocalDate date = LocalDate.parse(apiResult.get("AcceptedDate"));

Then I would format it to look how you want:

  1. SimpleDateFormat format = new SimpleDateFormat("HH:mm");
  2. String dateString = format.format( date ); // this string will be the time you are looking for

答案3

得分: 0

  1. fun formatDate(date: String): String {
  2. val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH)
  3. val output = SimpleDateFormat("12:08 PM", Locale.ENGLISH)
  4. return output.format(input.parse(date)!!)
  5. }
英文:

Kotlin

  1. fun formatDate(date: String): String {
  2. val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH)
  3. val output = SimpleDateFormat("12:08 PM", Locale.ENGLISH)
  4. return output.format(input.parse(date)!!)
  5. }

答案4

得分: 0

只需在字母"T"之后取下一个5个字符

  1. String date = "2020-09-28T11:47:37.217";
  2. int from = date.indexOf("T")+1;
  3. int to = from+5;
  4. String timeOfInterest = date.substring(from, to);
  5. System.out.println( timeOfInterest );
英文:

Just take the next 5 characters after the letter T

  1. String date = "2020-09-28T11:47:37.217";
  2. int from = date.indexOf("T")+1;
  3. int to = from+5;
  4. String timeOfInterest = date.substring(from, to);
  5. System.out.println( timeOfInterest );

答案5

得分: 0

  1. ## 使用 java.time 进行日期和时间操作,无论是通过解糖还是 ThreeTenABP
  2. 建议您考虑使用 java.time,这是现代的 Java 日期和时间 API,用于处理日期和时间。
  3. ```java
  4. String acceptedDate = "2020-09-28T11:47:37.217";
  5. LocalDateTime dateTime = LocalDateTime.parse(acceptedDate);
  6. String timeString = dateTime.toLocalTime()
  7. .truncatedTo(ChronoUnit.MINUTES)
  8. .toString();
  9. System.out.println(timeString);

输出结果为:

  1. > 11:47

问题:java.time 不是需要 Android API 级别 26 吗?

java.time 在较旧和较新的 Android 设备上都可以很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及较新的 Android 设备(从 API 级别 26 开始),现代 API 已内置。
  • 在非 Android 环境下的 Java 6 和 7 使用 ThreeTen Backport,这是现代类的后移版本(ThreeTen 用于 JSR 310;请参见底部的链接)。
  • 在较旧的 Android 上,可以使用解糖或 ThreeTen Backport 的 Android 版本。后者称为 ThreeTenABP。在后一种情况下,请确保从 org.threeten.bp 及其子包中导入日期和时间类。

链接

  1. <details>
  2. <summary>英文:</summary>
  3. ## java.time either through desugaring or through ThreeTenABP
  4. I suggest you consider using java.time, the modern Java date and time API, for your date and time work.
  5. String acceptedDate = &quot;2020-09-28T11:47:37.217&quot;;
  6. LocalDateTime dateTime = LocalDateTime.parse(acceptedDate);
  7. String timeString = dateTime.toLocalTime()
  8. .truncatedTo(ChronoUnit.MINUTES)
  9. .toString();
  10. System.out.println(timeString);
  11. Output is:
  12. &gt; 11:47
  13. ## Question: Doesn’t java.time require Android API level 26?
  14. java.time works nicely on both older and newer Android devices. It just requires at least **Java 6**.
  15. - In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  16. - 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).
  17. - 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.
  18. ## Links
  19. - [Oracle tutorial: Date Time](https://docs.oracle.com/javase/tutorial/datetime/) explaining how to use java.time.
  20. - [Java Specification Request (JSR) 310](https://jcp.org/en/jsr/detail?id=310), where `java.time` was first described.
  21. - [ThreeTen Backport project](http://www.threeten.org/threetenbp/), the backport of `java.time` to Java 6 and 7 (ThreeTen for JSR-310).
  22. - [Java 8+ APIs available through desugaring](https://developer.android.com/studio/write/java8-support-table)
  23. - [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), Android edition of ThreeTen Backport
  24. - [Question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project), with a very thorough explanation.
  25. </details>

huangapple
  • 本文由 发表于 2020年10月6日 22:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64227663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定