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

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

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

问题

通过 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"


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

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

"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"

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() 方法来创建一个实现此功能的方法:

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

以下是一个程序示例:

public class Main{

    public static void main(String []args){
        String time = "2020-09-28T11:47:37.217";
        System.out.println(stripRedundantDate(time));
    }
    
    public static String stripRedundantDate(String date){
        return date.substring(11, 16);
    }
}

产生的结果为:

11:47

如果你的日期长度不固定

使用以下方法:

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

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

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

The following program:

public class Main{

     public static void main(String []args){
        String time = "2020-09-28T11:47:37.217";
        System.out.println(stripRedundantDate(time));
     }
     public static String stripRedundantDate(String date){
         return date.substring(11, 16);
     }
}

Produces the result:

11:47

If your date is not a fixed length

Use the following:

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

答案2

得分: 0

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

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

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

SimpleDateFormat format = new SimpleDateFormat("HH:mm");

String dateString = format.format(date); // 这个字符串将是您要查找的时间
英文:

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

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

Then I would format it to look how you want:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");

String dateString = format.format( date  ); // this string will be the time you are looking for

答案3

得分: 0

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

Kotlin

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

答案4

得分: 0

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

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

Just take the next 5 characters after the letter T

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

答案5

得分: 0

## 使用 java.time 进行日期和时间操作,无论是通过解糖还是 ThreeTenABP

建议您考虑使用 java.time,这是现代的 Java 日期和时间 API,用于处理日期和时间。

```java
String acceptedDate = "2020-09-28T11:47:37.217";
LocalDateTime dateTime = LocalDateTime.parse(acceptedDate);
String timeString = dateTime.toLocalTime()
        .truncatedTo(ChronoUnit.MINUTES)
        .toString();
System.out.println(timeString);

输出结果为:

> 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 及其子包中导入日期和时间类。

链接


<details>
<summary>英文:</summary>

## java.time either through desugaring or through ThreeTenABP

I suggest you consider using java.time, the modern Java date and time API, for your date and time work.

		String acceptedDate = &quot;2020-09-28T11:47:37.217&quot;;
		LocalDateTime dateTime = LocalDateTime.parse(acceptedDate);
		String timeString = dateTime.toLocalTime()
				.truncatedTo(ChronoUnit.MINUTES)
				.toString();
		System.out.println(timeString);

Output is:

&gt; 11:47

## Question: Doesn’t java.time require Android API level 26?

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](https://docs.oracle.com/javase/tutorial/datetime/) explaining how to use java.time.
 - [Java Specification Request (JSR) 310](https://jcp.org/en/jsr/detail?id=310), where `java.time` was first described.
 - [ThreeTen Backport project](http://www.threeten.org/threetenbp/), the backport of `java.time` to Java 6 and 7 (ThreeTen for JSR-310).
 - [Java 8+ APIs available through desugaring](https://developer.android.com/studio/write/java8-support-table)
 - [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), Android edition of ThreeTen Backport
 - [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.


</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:

确定