• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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
0
AndyC1000
Asked:
AndyC1000
  • 2
  • 2
  • 2
2 Solutions
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now