0

我有一个date( Integer formatYYYYMMDD)。和 astart_time作为String (HH:mm 24 小时制)。并time_duration小时为单位double

int date = 20140214;
String start_time = "14:30";
double duration = 50.30;

我想使用这 3 个值并创建 2 个 Java 日期对象。一个是start_date,一个是end_date。它们的格式应为YYYY-MM-DD HH:mm.

然后在我得到 2 个数据字符串之后,例如 YYYY-MM-DD HH:mm. 我怎样才能获得那些以前的变量。date, start_time, duration.

这是我的尝试。

public void solve() throws IOException {
    int date = 20140214;
    String start_time = "14:30";
    double duration = 24.50;
    String startDate = "";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    startDate = getDate(date) + " " + start_time;
    try {
        Date start_Date = df.parse(startDate);
        Date end_Date = new Date(start_Date.getTime()+(int)(duration*3600*1000));
        System.out.println(df.format(start_Date));
        System.out.println(df.format(end_Date));

    } catch (ParseException ex) {

    }

}
public String getDate(int dateInt) {
    String date = "";
    String dateIntString = String.valueOf(dateInt);
    date = date + dateIntString.substring(0, 4) + "-";
    date = date + dateIntString.substring(4, 6) + "-";
    date = date + dateIntString.substring(6, 8);
    return date;
}

有什么简单的方法可以做到。? 或者我可以使用的一些内置功能,而不是我使用过的功能?

4

3 回答 3

1

Strange Data Types For Date-Time

Using:

  • An int to represent the digits of a calendar date
  • A string to represent time-of-day digits
  • A double to represent a duration of fractional hours

…are all unusual approaches. Probably not the wisest choices in handling date-time values.

Avoid java.util.Date/Calendar

Know that the bundled classes java.util.Date and .Calendar are notoriously troublesome and should be avoided. Use either Joda-Time or the new java.time.* package (Tutorial) in Java 8. And get familiar with the handy ISO 8601 standard.

Time Zone

Your question and example ignore the crucial issue of time zone. Handling date-time data without time zone is like handling text files without knowing their character encoding. Not good.

Use proper time zone names to create time zone object. Avoid the non-standard 3-letter codes.

Joda-Time

In Joda-Time, a DateTime object is similar to a java.util.Date object but actually knows its own assigned time zone.

Joda-Time offers three classes for representing spans of time: Period, Duration, and Interval.

The Interval class uses the "Half-Open" approach, where the beginning is inclusive and the ending is exclusive. This approach works well for handling spans of time and comparisons. Look for the handy contains, abuts, overlap, and gap methods.

int dateInput = 20140214;
String start_timeInput = "14:30";
double durationInput = 50.30;

// Clean up these inputs.
String datePortion = Integer.toString( dateInput );
String input = datePortion + " " + start_timeInput; 
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "yyyyMMdd HH:mm");

// Specify the time zone this date-time represents.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); // Or, DateTimeZone.UTC
DateTime dateTime = formatterInput.withZone( timeZone ).parseDateTime( input );

// Convert fractional hours to milliseconds, then milliseconds to a Duration object.
long millis = ( 60L * 60L * (long)(1000L * durationInput) ); // 1 hour = 60 minutes * 60 seconds * 1000 milliseconds.
Duration duration = new Duration( millis );

Interval interval = new Interval( dateTime, duration );

DateTimeFormatter formatterOutput = DateTimeFormat.forStyle( "MM" ).withLocale( Locale.FRANCE );
String description = "De " + formatterOutput.print( interval.getStart() ) + " à " + formatterOutput.print( interval.getEnd() );

Dump to console…</p>

System.out.println( "input: " + input );
System.out.println( "dateTime: " + dateTime );
System.out.println( "duration: " + duration ); // Format: PnYnMnDTnHnMnS (from ISO 8601)
System.out.println( "interval: " + interval ); // Format: <start>/<end> (from ISO 8601)
System.out.println( "description: " + description );   

When run…</p>

input: 20140214 14:30
dateTime: 2014-02-14T14:30:00.000+01:00
duration: PT181080S
interval: 2014-02-14T14:30:00.000+01:00/2014-02-16T16:48:00.000+01:00
description: De 14 févr. 2014 14:30:00 à 16 févr. 2014 16:48:00
于 2014-02-23T08:38:46.290 回答
0

你有很多日期的表示。

如有疑问,我通常会尽快达到 unix 标准时间(自 1970 年以来的毫秒数)。

在这种情况下,它将整数日期转换为字符串,首先读出四个作为年份,两个数字作为月份,最后两个作为日期,然后在 24 小时时间执行类似的操作,并创建一个java.util.Date 像这样:

SimpleDateFormat dateParser=new SimpleDateFormat("yyyyMMdd HH:mm"); //please double check the syntax for this guy...
String yyyyMmDd = date.toString();
String fullDate = yyyyMmDd + " " + start_time;
java.util.Date startDate = dateParser.parse(fullDate);
long startTimeInMillis = startDate.getTime();
final long MILLISECONDS_PER_HOUR = 1000*60*60;
long durationInMillis = (long)duration*MILLISECONDS_PER_HOUR;
java.util.Date endDate = new java.util.Date(startTimeInMillis + durationInMillis);

不要错过Joda 时间或 Java 8 新的、最终改进的名为java.time.

于 2014-02-23T07:37:56.030 回答
0
 you can write like
 int date = 20140214;
 String s=""+date;  
 Date date = new SimpleDateFormat("yyyyMMdd").parse(s);
于 2014-02-23T07:43:36.393 回答