简体   繁体   English

在Java中将时间元素存储在结构中

[英]Storing time elements in a structure in Java

I often need to manipulate time. 我经常需要操纵时间。 The Calendar class is good for working with dates and times but I can't seem to find a simple way of just storing elements like hours, minutes and seconds into some structure. Calendar类适用于处理日期和时间,但我似乎找不到一种简单的方法,只需将小时,分钟和秒等元素存储到某个结构中。 There seems to be a lot of conversion required from strings to actual ints. 从字符串到实际的整数似乎需要进行大量的转换。 I can always create my own class and just store the elements into fields but it would be nicer if a class exists that provides more functionality (adding time, etc) yet keeps it simple for storing and accessing. 我总是可以创建自己的类,只是将元素存储到字段中,但如果存在提供更多功能(增加时间等)但仍保持存储和访问简单的类会更好。 I'm looking for something like this: 我在寻找这样的东西:

Time time = new Time();
time.parse("13:30:15");
t.Hour = 13;
t.Minute = 30;
t.Second = 15;

Are there any Java classes that exist that do something simple like this. 是否存在任何像这样简单的Java类。 Here's how I do it now. 这就是我现在的表现。 What a horrific amount of code just to parse out hours and minutes: 解析小时和分钟是多么可怕的代码:

String time = "17:30";

TimeInfo timeInfo = new TimeInfo();
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
Date d = df.parse(time);

Calendar calendar = Calendar.getInstance();
calendar.setTime(d);

timeInfo.Hours = calendar.get(Calendar.HOUR_OF_DAY);
timeInfo.Minutes = calendar.get(Calendar.MINUTE);
timeInfo.Seconds = calendar.get(Calendar.SECOND);

class TimeInfo
{
  public int Hours;
  public int Minutes;
  public int Seconds;
  public boolean Is24HourFormat;
}

JodaTime is one of external libraries that help manipulate times easily. JodaTime是一个有助于轻松操纵时间的外部库。 You can add time. 你可以增加时间。 Also find the difference between times. 也找到时间的差异。

You also might want to check the difference between interval and duration which are concepts in jodatime lib. 您还可能想要检查intervalduration之间的差异,这些是jodatime lib中的概念。 [ SO post ] [ SO帖子 ]

Refer its javadoc for more info 有关详细信息,请参阅其javadoc

try this approach 尝试这种方法

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();
SimpleDateFormat f =new SimpleDateFormat("hh:mm:ss aa");
System.out.println(f.format(d));

I think the java.util.Calendar gives you everything you need. 我认为java.util.Calendar为您提供了所需的一切。 Have a look at Calendar#set() . 看看Calendar#set()

public final void set(int year, int month, int date, int hourOfDay, int minute, int second) public final void set(int year,int month,int date,int hourOfDay,int minute,int second)

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND. 设置字段YEAR,MONTH,DAY_OF_MONTH,HOUR,MINUTE和SECOND的值。 Previous values of other fields are retained. 保留其他字段的先前值。 If this is not desired, call clear() first. 如果不需要,请先调用clear()。

I would prefer to use DateUtils from Commons Lang. 我更喜欢使用Commons Lang的DateUtils。 This along with Calendar and DateFormat(SimpleDateFormat) should suffice all the date requirements in Java 这与Calendar和DateFormat(SimpleDateFormat)一起应该足以满足Java中的所有日期要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM