• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

TimeZone conversion problem

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. I am having a timezone conversion problem. I need to actually calculate the number of milliseconds that are passed after a specific time, and then subtract those milliseconds from the current time's milliseconds. if the value is negative, then i want my function to return -1, otherwise the actual milliseconds. The specific times when -1 is to be returned is monday through thursday: 1630 - 1730 (daily break for one hour)
friday 1630 to sunday 1730
public static long handleMarketClose()
{
System.out.println("DateUtil: inside handleMarketClose");
Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

if ((calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
System.out.println("setting current day to friday as today is saturday or sunday. see: " + calendar.get(Calendar.DAY_OF_WEEK));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
}
else
System.out.println("today is not saturday nor sunday. its " + calendar.get(Calendar.DAY_OF_WEEK));


//get the milliseconds past close time
long millisecondsSinceMarketClose = millisecondsPast(calendar.getTime(), TimeZone.getTimeZone(COESfxConstants.TIMEZONE));

//calculate the time to sleep
long timeToSleepUntilMarketOpens = 0L;

int currentDay = calendar.get(Calendar.DAY_OF_WEEK);
if (currentDay == Calendar.MONDAY ||
currentDay == Calendar.TUESDAY ||
currentDay == Calendar.WEDNESDAY ||
currentDay == Calendar.THURSDAY) {
System.out.println("after check: day of week is:" + currentDay);
timeToSleepUntilMarketOpens = 3600000L - millisecondsSinceMarketClose;
if(timeToSleepUntilMarketOpens >= 3600000L){
timeToSleepUntilMarketOpens = -1;
}
} else {
timeToSleepUntilMarketOpens = 176400000L - millisecondsSinceMarketClose;
if(timeToSleepUntilMarketOpens >= 176400000L){
timeToSleepUntilMarketOpens=-1;
}
}

System.out.println
("DateUtils: handleMarketClose: timeToSleepUntilMarketOpens: "
+ timeToSleepUntilMarketOpens + " milliseconds");

System.out.println
("DateUtils: handleMarketClose: timeToSleepUntilMarketOpens: "
+ (double) timeToSleepUntilMarketOpens / 3600000.0 + " hours");

return timeToSleepUntilMarketOpens;
}

private static long millisecondsPast(Date startDate, TimeZone timezone) {

long millisecondsPastStart = 0L;
Calendar startDateCalendar = Calendar.getInstance(timezone);
startDateCalendar.setTime(startDate);
int startDay = startDateCalendar.get(Calendar.DAY_OF_WEEK);
int startHours = startDateCalendar.get(Calendar.HOUR_OF_DAY);
int startMinutes = startDateCalendar.get(Calendar.MINUTE);
int startSeconds = startDateCalendar.get(Calendar.SECOND);
int startMillisecs = startDateCalendar.get(Calendar.MILLISECOND);
System.out.println("DateUtils: millisecondsPast: startDate: " + startDate);


now.set(Calendar.DAY_OF_WEEK, d.getDay());
now.set(Calendar.HOUR_OF_DAY, d.getHours());
now.set(Calendar.MINUTE, d.getMinutes());
now.set(Calendar.SECOND, d.getSeconds());
now.set(Calendar.MILLISECOND, 0);
now.setTime(d);
System.out.println("now is:" + now.getTime());
System.out.println("DateUtils: millisecondsPast: now: " + now.getTime());
int day = now.get(Calendar.DAY_OF_WEEK);
int hours = now.get(Calendar.HOUR_OF_DAY);
int minutes = now.get(Calendar.MINUTE);
int seconds = now.get(Calendar.SECOND);
int millisecs = now.get(Calendar.MILLISECOND);
int dayDiff = day - startDay;
if (dayDiff < 0) {
dayDiff += 7;
}
int hoursDiff = hours - startHours;
int minutesDiff = minutes - startMinutes;
int secondsDiff = seconds - startSeconds;
int millisecsDiff = millisecs - startMillisecs;

millisecondsPastStart
= (long) ((0) + (hoursDiff * 3600 * 1000)
+ (minutesDiff * 60 * 1000) + (secondsDiff * 1000)
+ (millisecsDiff));
System.out.println
("DateUtils: millisecondsPast: millisecondsPastStart: "
+ millisecondsPastStart);
System.out.println
("DateUtils: millisecondsPast: hours past market close time: "
+ (double) millisecondsPastStart / 3600000.0);

return millisecondsPastStart;
}
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People may not read a post like this entirely...you didn't really say what the problem is.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic