在 Java 中以 yyyymmddhhmmss.mmm 格式递增日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12092206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Incrementing date in yyyymmddhhmmss.mmm format in Java
提问by Cosmo Kramer
I want to increment the milliseconds in any given date in the format yyyymmddhhmmss.mmm in each iteration. mmm here represents milliseconds. And I want to perfom this operation in Java 1.5.
我想在每次迭代中以 yyyymmddhhmmss.mmm 格式增加任何给定日期的毫秒数。mmm 在这里代表毫秒。我想在 Java 1.5 中执行此操作。
For example: 20120823151034.567 should be incremented to 20120823151034.568
例如:20120823151034.567 应该增加到 20120823151034.568
回答by Peter Lawrey
You can use long
milli-seconds which make incrementing trivial.
您可以使用long
毫秒,这使得增量变得微不足道。
final String input = "20120823151034.567";
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date d = df.parse(input);
d.setTime(d.getTime()+1);
System.out.println(df.format(d));
I wouldn't use Calendar as its very slow.
我不会使用日历,因为它非常慢。
回答by gkuzmin
You can parse String
to Date
object and use getTime()
and setTime(long l)
to modify date. Then you can convert Date
object back to String
. For parsing String
and converting Date
object back to String
you can use SimpleDateFormat
class.
您可以解析String
到Date
对象,并使用getTime()
和setTime(long l)
修改日期。然后您可以将Date
对象转换回String
. 为了解析String
和转换Date
对象,String
您可以使用SimpleDateFormat
类。
回答by dasblinkenlight
The best class to use for this operation is Calendar
. You set it to the desired date, and then use
用于此操作的最佳类是Calendar
. 您将其设置为所需的日期,然后使用
myCalendar.add(Calendar.MILLISECOND, 1);
to advance it by one millisecond. Use DateFormat
to produce string representations.
将其提前一毫秒。使用DateFormat
来产生字符串表示。
回答by Marko Topolnik
This gives you what you want. It will work across any day/month/year boundary, as well as handling the start and end of daylight saving time.
这给了你你想要的。它将跨任何日/月/年边界工作,并处理夏令时的开始和结束。
final String input = "20120823151034.567";
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
final Calendar c = Calendar.getInstance();
c.setTime(df.parse(input));
c.add(Calendar.MILLISECOND, 1);
System.out.println(df.format(c.getTime()));
回答by Kumar Vivek Mitra
You can use Calendar Class as well as Date Class for this....
您可以为此使用日历类和日期类....
Date Class:
日期类别:
final String dateStr = "20120823151034.567";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date date = format.parse(input);
date.setTime(date.getTime()+1);
System.out.println(format.format(date));
Calendar Class:
日历类:
final String dateStr = "20120823151034.567";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(dateStr ));
c.add(Calendar.MILLISECOND,1);
System.out.println(format.format(c.getTime()));
In both cases, format.parse() has the potential to throw a ParseException, which you will need to catch and handle.
在这两种情况下,format.parse() 都有可能抛出 ParseException,您需要捕获并处理该异常。
回答by slim
An alternative without using Calendar (although, Calendar is fine)
不使用 Calendar 的替代方法(尽管 Calendar 很好)
final String input = "20120814120000.111";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date date = new format.parse(input);
long time = date.getTime();
Date incrementedDate = new Date(time + 1);
System.out.println(format.format(date));