I input a single date and obtain an ordered collection of (12-hour) timestamps where the first element is guaranteed to start on that date. The timestamps can potentially span multiple days given one input. I don't believe there are any cases where timestamps are more than 24 hours apart.

I need a way to handle the times and convert them to full dates in the case of rollover in a way such that the date increments properly.

Both the Date and Time are stored as Date objects. (my input is actually a sorted ArrayList<Date> as I have several hundred files to process)

For example:

> 2014.05.13

01:02:03 AM
10:54:21 PM
10:59:32 PM
11:34:00 PM
11:59:54 PM
12:01:00 AM
01:02:03 AM

I want a collection of Dates kind of like this:

[2014-05-13 01:02:03 AM,
2014-05-13 10:54:21 PM,
2014-05-13 10:59:32 PM,
2014-05-13 11:34:00 PM,
2014-05-13 11:59:54 PM,
2014-05-14 12:01:00 AM,
2014-05-14 01:02:03 AM]

What is a good way to create this collection? I am using Java 8.


Edit: thanks to the suggestion from https://stackoverflow.com/a/23646930/1467811 to add dates in a loop. Here's some pseudocode of my solution, since the parsing logic is a lot more complicated than this.

// 2014-01-21.165900-0500EST
// 2014-05-03.124529-0400EDT
DateFormat df = new SimpleDateFormat("yyyy-MM-dd.HHmmssZz", Locale.ENGLISH);
ArrayList<Date> logDates = new ArrayList<>();

// <snip> ... get array of dates ...

Collections.sort(logDates);

DateFormat msgDf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.ENGLISH);
Date lastDate;

for (Date d : logDates) {
    lastDate = d;
    Path p = GetFilePathFromDate(d);
    for (String line : Files.readAllLines(p)) { 
        String time = ExtractTimeFromLine(line); // ex. "12:01:00 AM"
        Date msgDate = msgDf.parse(new SimpleDateFormat("yyyy-MM-dd")
                      .format(lastDate) + " " + time);
        if (msgDate.before(lastDate)) { // must have passed over a day
            Calendar curCal = Calendar.getInstance();
            curCal.setTime(msgDate);
            curCal.add(Calendar.DAY_OF_MONTH, 1);

            msgDate = curCal.getTime();
        }
        lastDate = msgDate;

        // msgDate now has the correct date associated with its time
        // ex. "2014-05-14 12:01:00 AM"
        // do stuff with msgDate
    }
}
有帮助吗?

解决方案

Add some time in your date and add by loop in your collection ...

you can get help how to add some time in date by following link

Java : how to add 10 mins in my Time

 String myTime = "14:10";
 SimpleDateFormat df = new SimpleDateFormat("HH:mm");
 Date d = df.parse(myTime); 
 Calendar cal = Calendar.getInstance();
 cal.setTime(d);
 cal.add(Calendar.MINUTE, 10);
 String newTime = df.format(cal.getTime());

其他提示

Using a Set of Calendar?

Set<Calendar> dates = new HashSet<Calendar>();
// create specific calendar object
// add object to Set

First, it would be much better if your data source provided the time-of-day in standard ISO 8601 format using 24-hour clock, HH-MM-SS.

Even better, your data source should return the entire meaningful chunk of information, the date and the day-of-time and a time zone or offset altogether in standard ISO 8601 format, YYYY-MM-DDTHH:MM:SSZ. For example, 2016-09-26T21:57:18Z where the Z is short for Zulu and means UTC.

Anyways, to process your time-of-day values we must parse them. To parse, we must define a formatting pattern to match.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "hh:mm:ss aa" );

Loop your inputs, parsing each one, and collecting them. The LocalTime class represents a time-of-day without a date and without a time zone.

List<LocalTime> times = new ArrayList<>();
…
LocalTime lt = LocalTime.parse( input , formatter );
…

Define your initial date. The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate initialDate = LocalDate.parse( "2014-05-13" );

Loop your collection of LocalTime objects, combining each one with the LocalDate to get a LocalDateTime object.

LocalDateTime ldt = LocalDateTime.of( ld , lt );

But before combining, ask if the current LocalTime is less than the previous, is earlier. If so, increment the LocalDate.

if( lt.isBefore( previousLocalTime ) ) {
    ld = ld.plusDays( 1 );
…

Keep in mind that these LocalDateTime objects do not represent actual moments on the timeline. For an actual point in time, you must adjust them into a time zone (or offset).

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top