热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将Date.toString()转换回Java中的日期[复制]-ConvertDate.toString()backtoDateinJava[duplicate]

Thisquestionalreadyhasananswerhere:这个问题在这里已有答案:Java:howtoadd10minsinmyTi

This question already has an answer here:

这个问题在这里已有答案:

  • Java : how to add 10 mins in my Time 8 answers
  • Java:如何在我的Time 8答案中添加10分钟

  • How can I read and parse a date and time in Android? 2 answers
  • 如何在Android中阅读和解析日期和时间? 2个答案

I am calling an API and as per the guidelines calling it every time is inefficient as the data does not change that regularly. They recommend calling it once and then not polling until 10 minutes has passed.

我正在调用一个API,并且根据每次调用它的指南都是低效的,因为数据不会定期更改。他们建议调用一次,然后在10分钟后才进行轮询。

I am calling this from an Android app and so I want to store the current date plus 10 minutes. I do this like so:

我从一个Android应用程序调用它,所以我想存储当前日期加上10分钟。我是这样做的:

Date forecastRefreshDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(forecastRefreshDate);
cal.add(Calendar.MINUTE, 10);
editor.putString("ForecastRefreshDate", forecastRefreshDate.toString());
editor.apply();

So now when this code is run again it needs to check if the current time (new Date()) is > the value saved in the cache/app file.

所以现在再次运行此代码时,需要检查当前时间(new Date())是否>缓存/ app文件中保存的值。

How do I create a Date variable equal to the value stored in the cache as a String?

如何创建一个Date变量,该变量等于存储在缓存中的值为String?

5 个解决方案

#1


Convert date to epoch time, which is a number of milliseconds stored as a long. Store long as string and parse back into long when needed.

将日期转换为纪元时间,这是一个以毫秒为单位存储的毫秒数。存储为字符串长,并在需要时解析为long。

long epoch = date.getTime(); //where date is your Date
String yourString = Long.toString(epoch); //to string for storage
long time = Long.valueOf(yourString).longValue(); ; //back to epoch
Date originaldate = new Date(Long.parseLong(time)); //back to date

#2


When dealing with time, which is needed only internally to measure time periods, It is much better to store timestamps as long value (millis sicne 1.1.1970 UTC).

处理时间时,只需要在内部测量时间段,最好将时间戳存储为长值(millis sicne 1.1.1970 UTC)。

Use long timestamp = System.currentTimeMillis()

使用long timestamp = System.currentTimeMillis()

If a String storage is neccesary simply convert the Long to a String.

如果需要String存储,只需将Long转换为String。

String timeStampStr = String.parseLong(timeStamp);

Do not use Date.toString for other things than for logging or debugging. The toString() representation may be different in other countries. If a human readable timestamp string is needed you have to specify a Specific DateFormat, see also DateFormatter.

除了记录或调试之外,不要使用Date.toString。 toString()表示在其他国家可能有所不同。如果需要人类可读的时间戳字符串,则必须指定特定的DateFormat,另请参见DateFormatter。

#3


Parsing a long it the most efficent way, however there may be cases that you have already formatted a date to some other format and want to parse that. Then you should use this

解析它是最有效的方式,但是可能有些情况下你已经将日期格式化为其他格式并想要解析它。那你应该用它

private static final String format = "yyyy-MM-dd HH:mm:ss";
private static final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
private Calendar dateTime = Calendar.getInstance();

public void setDateTime(String dateTimeString) {
    try {
        dateTime.setTime(formatter.parse(dateTimeString));
    } catch (ParseException e) {
         // Dummy handled exception
        dateTime.setTimeInMillis(0);
    }
}

Parse a long containing milliseconds since epoch

从epoch开始,解析一段长的毫秒

Date date = new Date(Long.parseLong(timeInMillisecondsString));

#4


For date conversions you can use DateFormat class - read about parse method

对于日期转换,您可以使用DateFormat类 - 阅读有关解析方法的信息

#5


Since you're using a plain toString() it should be coverable using a SimpleDateFormat.

由于您使用的是普通的toString(),因此使用SimpleDateFormat应该可以使用它。

Try this:

DateFormat dateFormat = new SimpleDateFormat();
Date myDate = dateFormat.parse(myDateString);

推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Vagrant虚拟化工具的安装和使用教程
    本文介绍了Vagrant虚拟化工具的安装和使用教程。首先介绍了安装virtualBox和Vagrant的步骤。然后详细说明了Vagrant的安装和使用方法,包括如何检查安装是否成功。最后介绍了下载虚拟机镜像的步骤,以及Vagrant镜像网站的相关信息。 ... [详细]
  • 本文介绍了Python语言程序设计中文件和数据格式化的操作,包括使用np.savetext保存文本文件,对文本文件和二进制文件进行统一的操作步骤,以及使用Numpy模块进行数据可视化编程的指南。同时还提供了一些关于Python的测试题。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 怀疑是每次都在新建文件,具体代码如下 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文详细介绍了git常用命令及其操作方法,包括查看、添加、提交、删除、找回等操作,以及如何重置修改文件、抛弃工作区修改、将工作文件提交到本地暂存区、从版本库中删除文件等。同时还介绍了如何从暂存区恢复到工作文件、恢复最近一次提交过的状态,以及如何合并多个操作等。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了在Android开发中使用软引用和弱引用的应用。如果一个对象只具有软引用,那么只有在内存不够的情况下才会被回收,可以用来实现内存敏感的高速缓存;而如果一个对象只具有弱引用,不管内存是否足够,都会被垃圾回收器回收。软引用和弱引用还可以与引用队列联合使用,当被引用的对象被回收时,会将引用加入到关联的引用队列中。软引用和弱引用的根本区别在于生命周期的长短,弱引用的对象可能随时被回收,而软引用的对象只有在内存不够时才会被回收。 ... [详细]
  • 本文介绍了H5游戏性能优化和调试技巧,包括从问题表象出发进行优化、排除外部问题导致的卡顿、帧率设定、减少drawcall的方法、UI优化和图集渲染等八个理念。对于游戏程序员来说,解决游戏性能问题是一个关键的任务,本文提供了一些有用的参考价值。摘要长度为183字。 ... [详细]
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
author-avatar
庾承泓_689
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有