通过解析输入将日期添加到现在 [英] Add a date to now by parsing the input

查看:100
本文介绍了通过解析输入将日期添加到现在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将来尝试解析日期时遇到了困难,希望能提供一些帮助!

I'm having an hard time trying to parse date in the future, and I would apreciate some help!

这就是问题,我想将一个已解析的日期添加到当前日期(以备将来使用).问题是我可以使用多种日期格式,例如:

Here's the thing, I'd like to add a parsed date to the current date (to have it in the future). The problem is that I can have many kind of date format, like :

  • dd
  • MM-dd
  • yyyy-MM-dd

因此,如果用户设置了类似5的内容,则返回的日期将为(在我们的游戏中时光倒流)2011-11-05. 如果他设置了02-14,它将是2012-02-14.

So if the user set something like 5, the returned date will be (in our timelapse) 2011-11-05. If he set 02-14, it will be 2012-02-14.

但是假设我们是4th of november,并且用户设置了11-03,它将是2012-11-03,而不是2011-11-03,因为它已经过去了.

But suppose we are the 4th of november, and the user set 11-03, it will be 2012-11-03 and not 2011-11-03 since it's past.

我尝试使用Calendar,Date,SimpleFormat,但无法使其正常工作.

I tried to play with Calendar, Date, SimpleFormat, but I cannot make it work.

我的解析器(使用SimpleDateFormat)正在运行.

My parsers (using SimpleDateFormat) are working though.

您能帮我存档吗?我并不需要完整的代码,只是让我踏上正确的道路!

Could you help me archieve this? I'm not asking for a complete code, just something that would set me on the right track!

谢谢! :)

推荐答案

我在jodatime方面有很好的经验- http://joda-time.sourceforge.net/.在其中签出Dateformatters.

I have had very good experience with jodatime - http://joda-time.sourceforge.net/. Checkout the Dateformatters in that.

它具有非常广泛的API,可让您执行诸如添加和减去日期之类的操作-考虑到时区和夏时制等.

It has a very extensive API and lets you do things like add and subtract dates - taking into account timezones and daylight saving etc.

这篇关于通过解析输入将日期添加到现在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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