将人类日期(当地时间GMT)转​​换为日期 [英] Convert Human Date (Local Time GMT) to date

查看:112
本文介绍了将人类日期(当地时间GMT)转​​换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在服务器上工作,并且服务器正在将我的格林威治标准时间本地日期发送给我,例如字符串格式的Fri Jun 22 09:29:29 NPT 2018,然后将其转换为日期,如下所示:

I am working on server and server is sending me date on GMT Local Date like Fri Jun 22 09:29:29 NPT 2018 on String format and I convert it into Date like below:

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.English);
Date newDate=simpleDateFormat.parse("Fri Jun 22 09:29:29 NPT 2018");
TimeAgo ta=new TimeAgo();
Log.d(TAG,""+ta.timeAgo(newDate));

我需要的是像5 hours ago那样在Ago中取出时间,因为我使用了

What I need is take out the Time in Ago like 5 hours ago for that I use the one github project on which returns TimeAgo on passing date.

我已经查看了此答案但不能解决我的问题.

I have already look at this answer but didn't solve my problem.

异常:Err java.text.ParseException:不可解析的日期:"NPT 2018年6月22日星期五09:29:29"(偏移量为20) 错误java.text.ParseException:无法解析的日期:周五6月22日09:29:29 NPT"(偏移量为20)

Exception: Err java.text.ParseException: Unparseable date: "Fri Jun 22 09:29:29 NPT 2018" (at offset 20) Err java.text.ParseException: Unparseable date: "Fri Jun 22 09:29:29 NPT 2018" (at offset 20)

推荐答案

NPT不被识别为时区缩写

问题是您的日期时间字符串(显然是Date.toString()的输出)的解析(不是随后使用TimeAgo的原因,您可以将其排除在问题之外,以使其更清楚).不可解析的部分在索引20处,即NPT的位置,我指的是尼泊尔时间.因此,Android设备或模拟器上的SimpleDateFormat不会将NPT识别为时区缩写.

NPT is not recognized as a time zone abbreviation

The parsing of your date-time string (apparently the output from Date.toString()) is the problem (not the subsequent use of TimeAgo, which you could have left out from the question to make it clearer). The unparseable part is at index 20, that is where it says NPT, which I take to mean Nepal Time. So SimpleDateFormat on your Android device or emulator doesn’t recognize NPT as a time zone abbreviation.

时区缩写是语言环境数据的一部分.我不是Android开发人员,也不知道Android从何处获取其区域设置数据.快速网络搜索提到了ICU和CLDR.您可以进行更彻底的搜索,并且无疑会找到我没有找到的信息.

Time zone abbreviations come as part of the locale data. I am not an Android developer and don’t know from where Android gets its locale data. A fast web search mentioned ICU and CLDR. You can search more thoroughly and no doubt find information I didn’t find.

我提出了三个建议供您尝试.我立即承认前两者不太可能解决您的问题,但是我仍然发现它们值得尝试.我保证,如果前两个都不起作用,那么第三个会起作用.

I am presenting three suggestions for you to try. I admit at once that the first two are unlikely to solve your problem, but I nevertheless find them worth trying. And I promise that the third will work if the first two don’t.

我同意 notyou的答案,我们认为类DateSimpleDateFormat已过时,并且效果更好使用java.time(现代Java日期和时间API).您可以在Android O之前的Android上执行此操作吗?是的,java.time的大多数已被反向移植.反向端口的Android版本称为ThreeTenABP.使用底部的链接.然后尝试:

I agree with the answer by notyou that the classes Date and SimpleDateFormat are outmoded and that it’s better to use java.time, the modern Java date and time API. Can you do that on Android prior to Android O? Yes, most of java.time has been backported. The Android edition of the backport is called ThreeTenABP. Use the links at the bottom. Then try:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
    ZonedDateTime newDateTime
            = ZonedDateTime.parse("Fri Jun 22 09:29:29 NPT 2018", formatter);
    System.out.println(newDateTime);

确保将导入用于反向端口:

Make sure you use the imports for the backport:

import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;

我已经使用相同的反向端口进行了测试,但没有测试过Android版本.我得到了:

I have tested with the same backport, only not the Android edition. I got:

2018-06-22T09:29:29 + 05:45 [亚洲/加德满都]

2018-06-22T09:29:29+05:45[Asia/Kathmandu]

我怀疑ThreeTenABP使用相同的语言环境数据,如果这样的话,这并不能解决您的问题.

I suspect that ThreeTenABP uses the same locale data, though, and if so, this doesn’t solve your problem.

    DateTimeFormatter formatter = DateTimeFormatter
            .ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT)
            .withZone(ZoneId.of("Asia/Kathmandu"));

如果可以,我觉得它简单明了.如果您坚持使用SimpleDateFormat,则可以尝试使用类似的技巧.我得到与上面相同的输出.

If it works, I find it straightforward and clean. If you insist on using SimpleDateFormat, you can try a similar trick with it. I get the same output as above.

这是一个hack:要求字符串中出现三个字母NPT,而不将它们解释为时区.这样就无需将缩写识别为时区,因此可以使用.

This is a hack: require that the three letters NPT occur in the string without interpreting them as a time zone. This eliminates the need for the abbreviation to be recognized as a time zone, so will work.

    DateTimeFormatter formatter = DateTimeFormatter
            .ofPattern("EEE MMM dd HH:mm:ss 'NPT' yyyy", Locale.ROOT)
            .withZone(ZoneId.of("Asia/Kathmandu"));

我们还需要设置时区,因为现在这是Java唯一可以从中获取时区的地方.

We also need to set the time zone since this is now the only place Java can get the time zone from.

要为TimeAgo获取老式的Date对象,请进行如下转换:

To obtain an old-fashioned Date object for TimeAgo, convert like this:

    Date newDate = DateTimeUtils.toDate(newDateTime.toInstant());

链接

  • Oracle教程:日期时间解释了如何使用java.time. /li>
  • Java规范请求(JSR)310 ,其中java.time是首先描述.
  • ThreeTen Backport项目,它是Java 6和7的java.time的反向端口( JSR-310).
  • ThreeTenABP ,ThreeTen Backport的Android版本
  • 问题:如何在Android Project中使用ThreeTenABP ,非常详尽的解释.
  • 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.timeto Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    • 这篇关于将人类日期(当地时间GMT)转​​换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆