Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <code>getTimeZone.getDefault</code> combined with according to the <a href="http://developer.android.com/reference/java/util/TimeZone.html" rel="noreferrer">Android documentation.</a></p> <blockquote> <p>public static synchronized TimeZone getDefault ()</p> <p>Gets the default time zone. Returns the default time zone.</p> </blockquote> <p>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)).</p> <p>So really you just need to get the local timezone offset and feed it into the basic equation: <code>TimeZone.getDefault + 6</code> and you'll know what to add or subtract to your local DB. (+6 since -(-6) always works out to +6).</p> <p>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.</p> <h2>Crude Attempt at Java</h2> <p>I already said I have no idea how to do Java or object oriented anything, right?</p> <p>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:</p> <pre><code>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. </code></pre> <p>Anywhere close to how to do this in java?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload