This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes TimeZone conversion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "TimeZone conversion problem" Watch "TimeZone conversion problem" New topic
Author

TimeZone conversion problem

Murad Iqbal
Ranch Hand

Joined: Dec 09, 2003
Posts: 90
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;
}
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
People may not read a post like this entirely...you didn't really say what the problem is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TimeZone conversion problem
 
Similar Threads
java.util.Calendar, setTime and DST
Setting the time
Calendar Object - updateTime()
Get and print elapsed time
Woa! Why does Date/Calendar do this?