Generate timesteps to start at 00:30 and end at 00:00

Dear all,

I am trying to change the below method StoreInMapRandom, it generates 30 minute timesteps between two dates and stores the data in a map.  This solution is from a previous post where the first timestep is 00:00, it needs to be 00:30.  I added 30 minutes in milliseconds to the long start it now starts at 00:30 but ends at 11:30.  

Thanks

// parse dates into longs
		long startDate = new SimpleDateFormat("dd/MM/yyyy").parse("13/03/2015")
				.getTime();
		startCal = Calendar.getInstance();
		startCal.setTimeInMillis(startDate);

		long endDate = new SimpleDateFormat("dd/MM/yyyy").parse("15/03/2015")
				.getTime();
		endCal = Calendar.getInstance();
		endCal.setTimeInMillis(endDate);

// map contains long and random number
			Map<Long, Double> timeseries_map = util.storeInMapRandom(startDate,
					endDate);

public Map<Long, Double> storeInMapRandom(long start, long end) {
		RandomNumberGenerator ra = new RandomNumberGenerator();
		Map<Long, Double> map = new TreeMap<>();
		// long timeMS = start; // first timestep is 00:00 
		long timeMS = start + 1800000; // start at 00:30 (12.30AM) 
		while (timeMS < end) {
			// Store the data in the map
			map.put(timeMS, ra.getRandomNumber());

			// Move ahead 30 mins in milliseconds
			timeMS += TimeUnit.MILLISECONDS.convert(30L, TimeUnit.MINUTES);
			// System.out.println(new Date(timeMS));
		}
		return map;
	}

Open in new window

AndyC1000Asked:
Who is Participating?
 
dpearsonCommented:
What Jeff suggests should work fine.

If you just want to offset all of the generated times by 30 mins you could also switch the order of these 2 lines inside the loop (and take out the line where you added 30 mins).

                  // Move ahead 30 mins in milliseconds
                  timeMS += TimeUnit.MILLISECONDS.convert(30L, TimeUnit.MINUTES);

                  // Store the data in the map
                  map.put(timeMS, ra.getRandomNumber());

public Map<Long, Double> storeInMapRandom(long start, long end) {
		RandomNumberGenerator ra = new RandomNumberGenerator();
		Map<Long, Double> map = new TreeMap<>();
		long timeMS = start; // first timestep is 00:00 
		while (timeMS < end) {
			// Move ahead 30 mins in milliseconds
			timeMS += TimeUnit.MILLISECONDS.convert(30L, TimeUnit.MINUTES);

			// Store the data in the map
			map.put(timeMS, ra.getRandomNumber());

			// System.out.println(new Date(timeMS));
		}
		return map;
	}

Open in new window

Doug
0
 
Jeffrey Dake Senior Director of TechnologyCommented:
Change you while loop to be

While (timeMS <= end)

Right now you have it ending when the times are equal, but sounds like you still want the loop to execute when it is at the end time.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.