views:

818

answers:

2

I have a MySql database that stores a timestamp for each record I insert. I pull that timestamp into my Android application as a string. My database is located on a server that has a TimeZone of CST. I want to convert that CST timestamp to the Android device's local time.

Can someone help with this?

A: 

Hi, can't you simply convert the date with simpleDateFormat? then you just define the structure of your incoming date like that (df) and transform it to the form you want (df):

private static DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
private static DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm");

public void setyourDate(String yourDate) {
Date date2;
yourDate = getyourDate() + "" + yourDate;
try {
date2 = df.parse(yourDate);
yourDate = df2.format(date2);

} catch (ParseException e) {
}
this.yourDate = yourDate;
}

does it make sense?

Sephy
I think this will work nicely when I want to show the timestamp in a different format. I am looking to convert my timestamp from a CST timezone to the users local timezone.
chedstone
A: 

Use getTimeZone.getDefault combined with according to the Android documentation.

public static synchronized TimeZone getDefault ()

Gets the default time zone. Returns the default time zone.

So since you know that CST is -6:00 from GMT, and you get a local timezone saying the user is +9:00 (Japan), you'd know to adjust your MySQL DB times by +15 hours (9 - (-6)). Or if they are in Miami (EST, -5), you would adjust by adding one hour (-5 - (-6)). If the are in Portland, Oregon, (PST -8), you would subtract 2 hours (-8 -(-6)).

So really you just need to get the local timezone offset and feed it into the basic equation: TimeZone.getDefault + 6 and you'll know what to add or subtract to your local DB. (+6 since -(-6) always works out to +6).

If I knew the first thing about writing Java, I'd go the extra step and write a bit of sample code, but alas, I'm only smart enough for scripts.

Crude Attempt at Java

I already said I have no idea how to do Java or object oriented anything, right?

Here's a crude attempt from just poking around the Android documentation. Any fine points or simple "Not even close" remarks welcome. Bear in mind that I figured out the right method and class already just from a quick search and I came up with a simple equation for converting the timezone offset for anywhere to CST, so I'm not a dunce, just someone who doesn't know when to leave well enough alone. Anyway, crude attempt:

System now = System.currentTimeMillis (); //Gets current local time in ms
TimeZone local_tz = TimeZone.getDefault();  //Gets current local TZ of phone
tz_offset_gmt = local_tz.getOffset(now)/3600000; // Get Offset in ms, divide by 3600000
tz_offset_cst = tz_offset_gmt + 6;  // add offset to 6 to get current TZ offset to CST.

Anywhere close to how to do this in java?

Anthony
Perfect perfect! This makes sense. I thought maybe there was a function that would automagically do ti for me. You and @Sephy have been most helpful!
chedstone