在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:33:21  来源:igfitidea点击:

Incrementing date in yyyymmddhhmmss.mmm format in Java

javadate

提问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 longmilli-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 Stringto Dateobject and use getTime()and setTime(long l)to modify date. Then you can convert Dateobject back to String. For parsing Stringand converting Dateobject back to Stringyou can use SimpleDateFormatclass.

您可以解析StringDate对象,并使用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 DateFormatto 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));