OneSeven: How to get the time zone

click to enlarge

Over the weekend I got bored with my homescreen. I tried several widgets like BattStatt, Tajm, dClock, and TypoClock, but they all took up at least 2 rows of my homescreen, or I didn’t need the information. I loved dClock’s week display since the actual day isn’t obviously visible on Android anywhere but the lock screen. So I went ahead and made my own simple week widget over the weekend.

Obviously, the week widget needs to display the correct day to the user, which can vary depending on time zone. It should also know when midnight/the start of the new day is, so that it may change. I’ll share with you how to get the current time relevant to the user.

 * The private getDay method gets the current time zone and then returns
 * the integer day of the week for this time zone.
 * @return		The day of the week (see Calendar.SUNDAY)
private int getDay() {
	 TimeZone t = TimeZone.getDefault(); //gets the phone's curr timezone
	 // create a our current time zone
	 SimpleTimeZone pdt = new SimpleTimeZone(t.getRawOffset(), t.getID());
	 // set up rules for daylight savings time for 2010, US
	 pdt.setStartRule(Calendar.MARCH, 14, 2 * 60 * 60 * 1000);
	 pdt.setEndRule(Calendar.NOVEMBER, 7, 2 * 60 * 60 * 1000);
	 // create a GregorianCalendar with the Pacific Daylight time zone
	 // and the current date and time
	 calendar = new GregorianCalendar(pdt);
	 Date trialTime = new Date();
	return calendar.get(Calendar.DAY_OF_WEEK);

This method returns the current day of the week depending on the user’s timezone. The Daylight Savings is adjusted to this year’s US date’s, but you could easily add clauses to change it for other countries.

Now that you have the day, you can easily calculate where midnight will be (either by raw calculation or just create a new calendar and bump it forward to the desired date/time). I used AlarmManager to set a repeating alarm every day.

OneSeven is available in free and pro versions. Please click here to view the features of each.

2 thoughts on “OneSeven: How to get the time zone

Comments are closed.