Java generate and store half hourly time steps from start to end date

Dear all,

I'm trying to convert an excel model into java.  I need to generate half hourly time steps based on a start and end date provided by the user and store into a data structure.  I've converted the date into simple date format dd-MM-yyyy.  What is the most efficient way to generate this data and store in the data structure below?

I've decided to store the data in nested Maps, i.e. (if there is a better way let me know...).  Weather is an example of one set of data, there are 5 others that will need to be accessed in the same way.

Map<Date, Map<String, double>> weatherMap = new HashMap<Date, HashMap<String, double>>();

The inner map contains the timestep as key, and weather data as value.  The outer map will contain the date as key and the inner map as value.

I've defined the timestep value as type string, not sure if it should be double?

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

CEHJCommented:
If there are no 'holes' in the data, then the keys are redundant as the correct data point can be determined by calculation. All that's needed is start date, end date and a list of doubles
0
AndyC1000Author Commented:
I need to be able to store the data there will be a number of calculations based on a date/ timestep.  For each date/time step I will be running a calculation 1000 times (monte carlo).
0
dpearsonCommented:
Dates are easy to work with in Java as longs (which means they're measured in milliseconds and are GMT - so no time zone nonsense).

So the map would become:

Map<Date, Map<Long, Double>> weatherMap ;

But the outer "Date" object is actually redundant - because a "Long" is milliseconds since 1970 - it's any date down to the millisecond.  So you really just need:

Map<Long, Double> weatherMap ;

This assumes that given a specific Date+time you'll round it off to the nearest 30 mins and then do the lookup in the map.

Here's some code to get you started on generating the map - it takes a start and end date, rounds them to the nearest hour and then generates entries for every 30 mins.  You'll still need to write the code to do the lookup - which is similar but rather than truncating to the nearest hour, you need to round off to the nearest 30 mins.

Hope that helps,

Doug

	private Date clearMinutes(Date date) {
		Calendar startCal = Calendar.getInstance() ;
		startCal.setTime(date);

		startCal.set(Calendar.MINUTE, 0);
		startCal.set(Calendar.SECOND, 0);
		startCal.set(Calendar.MILLISECOND, 0);
		return startCal.getTime() ;
	}

	private Map<Long, Double> storeInMap(Date start, Date end) {
		Map<Long, Double> map = new HashMap<>() ;

		start = clearMinutes(start) ;
		end = clearMinutes(end) ;

		long timeMS = start.getTime();
		while (timeMS < end.getTime()) {
			// Store the data in the map
			map.put(timeMS, 1.0);

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

			// Sanity check for what date we're on in a human readable form (remove this later)
			System.out.println(new Date(timeMS));
		}

		return map ;
	}

Open in new window

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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

CEHJCommented:
I need to be able to store the data there will be a number of calculations based on a date/ timestep.  For each date/time step I will be running a calculation 1000 times (monte carlo).
I still  don't see why you'd need to store anything, other than perhaps the results of the calculations
0
AndyC1000Author Commented:
Thanks.  I understand now why the date is redundant didn't know long values contained date info.

I'm not sure what you meant by the below rounding off instead of truncating.
You'll still need to write the code to do the lookup - which is similar but rather than truncating to the nearest hour, you need to round off to the nearest 30 mins.
0
dpearsonCommented:
I just meant that if you looked for the weather data at 12:29pm I think you'd expect to get the value from 12:30pm not from 12:00pm - so we'd be rounding off the time to the nearest 30 mins.

Does that make sense?  It's just a small adjustment to the code.  You would want something different from this:

startCal.set(Calendar.MINUTE, 0);

when doing the lookup.  You'd want to consider the current minute value and round it to the nearest 30, something like:

// Round time to the nearest 30 mins
int mins = startCal.get(Calendar.MINUTE) ;
if (mins > 15 && mins < 45)
   mins = 30 ;
else
   mins =  0;
startCal.set(Calendar.MINUTE, mins);

Hope that helps,

Doug
0
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.