使用美国日期和时间以及递增操作。

huangapple go评论75阅读模式
英文:

Working with U.S. date and time and Incrementing

问题

我想创建一个函数,接受一个字符串,表示以“MM/dd/yyyy HH:mm AM or PM”格式表示的日期,我想在将其以“yyyy-MM-dd HH:mm AM or PM”格式返回之前,将日期和时间增加X天和时间X次。目前我唯一知道的方法是将其转换为欧洲日期和时间,在使用LocalDateTime或Calendar进行操作,然后再次转换,但来回转换感觉效率低下。是否有可以处理美国时间并执行诸如增加等操作的日期和时间对象呢?

英文:

I want to create a function that takes in a string representing a date in the format "MM/dd/yyyy HH:mm AM or PM" and I want to increment days and time X number of times before it is returned in the format yyyy-MM-dd HH:mm AM or PM". Right now the only way I know of is to convert it to European date and time, work with it using LocalDateTime or Calendar and then convert it again, but it feels inefficient converting back and forth. Are there any date and time objects that can work with US time and do things like increments?

答案1

得分: 1

尝试使用 DateTimeFormatter.ofPattern(...) 将您的字符串解析为 LocalDateTime。还有解析上午/下午的选项。

符号     含义                      格式化方式       示例
------  -------                     ------------      -------
a       上午/下午                   文本              下午

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO

英文:

try DateTimeFormatter.ofPattern(...) to parse your string to a LocalDateTime. There are also options for parsing AM/PM

Symbol  Meaning                     Presentation      Examples
------  -------                     ------------      -------
a       am-pm-of-day                text              PM

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO

答案2

得分: 1

import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm:ss a", Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.parse("05/06/2020 3:45:00 PM", formatter);
dateTime.plus(5, ChronoUnit.DAYS);
英文:

DateTimeFormatter + LocalDateTime

import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

DateTimeFormatter formater = DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm:ss a", Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.parse("05/06/2020 3:45:00 PM", formater);
dateTime.plus(5, ChronoUnit.DAYS)

答案3

得分: 0

由于您一开始将日期作为字符串处理,您需要进行转换为LocalDateTime以进行操作。话虽如此,您可以像这样操作:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a");
LocalDateTime date = LocalDateTime.parse([yourString], formatter);

从这里开始,您可以使用此文档中的加法和减法方法进行日期/时间操作:https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
然后,一旦完成操作,可以创建一个SimpleDateFormat,如下所示:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

然后像这样打印您的字符串:

sdf.format(date).toString();
英文:

Since you are taking in the date as a String in the first place you will need to do a conversion to LocalDateTime to manipulate it. That being said, you could do something like this:

    DateTimeFormatter formatter = new DateTimeFormatter("MM/dd/yyyy hh:mm a");
    LocalDateTime date = LocalDateTime.parse([yourString], formatter);

From here you would do your date/time manipulation using the plus and minus methods from this documentation: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
Then once you have done that create a SimpleDateFormat like so:

SimpleDateFormat sdf = new SimpleDateFormatter.ofPattern(yyyy-MM-dd hh:mm a)

and then print your string like so:

sdf.format(date).toString(); 

huangapple
  • 本文由 发表于 2020年5月5日 21:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61614514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定