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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.