Localize dates. Use DateFormat.getDateInstance for

user visible dates.
This commit is contained in:
Jimmy Shih
2011-11-17 20:10:57 -08:00
parent ce64da7849
commit 5aea1348f6
12 changed files with 48 additions and 80 deletions
@@ -32,7 +32,7 @@ import android.os.RemoteException;
import android.util.Log;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
@@ -44,8 +44,7 @@ import java.util.TimerTask;
*/
public class SensorStateActivity extends Activity {
private static final SimpleDateFormat TIMESTAMP_FORMAT =
new SimpleDateFormat("HH:mm:ss");
private static final DateFormat TIMESTAMP_FORMAT = DateFormat.getTimeInstance(DateFormat.SHORT);
private static final long REFRESH_PERIOD_MS = 250;
private final StatsUtilities utils;
@@ -32,7 +32,7 @@ import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
@@ -47,8 +47,8 @@ import java.util.Date;
public class BackupActivityHelper {
// Since the user sees this format, we use the local timezone
private static final SimpleDateFormat DISPLAY_BACKUP_FORMAT =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final DateFormat DISPLAY_BACKUP_FORMAT = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
private static final Comparator<Date> REVERSE_DATE_ORDER =
new Comparator<Date>() {
@@ -34,7 +34,6 @@ import com.google.wireless.gdata.parser.ParseException;
import com.google.wireless.gdata2.client.AuthenticationException;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;
import java.io.BufferedReader;
@@ -45,6 +44,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;
@@ -290,7 +290,7 @@ public class DocsHelper {
DocsTagBuilder tagBuilder = new DocsTagBuilder(metricUnits)
.append("name", track.getName())
.append("description", track.getDescription())
.append("date", getDisplayDate(context, stats.getStartTime()))
.append("date", getDisplayDate(stats.getStartTime()))
.append("totaltime", StringUtils.formatTimeAlwaysShowingHours(
stats.getTotalTime()))
.append("movingtime", StringUtils.formatTimeAlwaysShowingHours(
@@ -331,17 +331,15 @@ public class DocsHelper {
}
/**
* Gets the display string for a time based on the phone's setting.
* Gets the display string for a time.
*
* @param context the context to obtain the phone's setting.
* @param time the time
* @return the display string of the time
*/
protected String getDisplayDate(Context context, long time) {
java.text.DateFormat dateFormat = DateFormat.getDateFormat(context);
java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context);
private String getDisplayDate(long time) {
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Date startTime = new Date(time);
String dateString = dateFormat.format(startTime) + " " + timeFormat.format(startTime);
String dateString = format.format(startTime);
return dateString;
}
@@ -43,12 +43,12 @@ import java.text.NumberFormat;
*/
class DocsTagBuilder {
private static final NumberFormat LARGE_UNIT_FORMAT = NumberFormat.getIntegerInstance();
private static final NumberFormat SMALL_UNIT_FORMAT = NumberFormat.getNumberInstance();
private static final NumberFormat LARGE_UNIT_FORMAT = NumberFormat.getNumberInstance();
private static final NumberFormat SMALL_UNIT_FORMAT = NumberFormat.getIntegerInstance();
static {
SMALL_UNIT_FORMAT.setMaximumFractionDigits(2);
SMALL_UNIT_FORMAT.setMinimumFractionDigits(2);
LARGE_UNIT_FORMAT.setMaximumFractionDigits(2);
LARGE_UNIT_FORMAT.setMinimumFractionDigits(2);
}
protected final boolean metricUnits;
@@ -18,15 +18,14 @@ package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.util.FileUtils;
import android.location.Location;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Exports a track as a CSV file, according to RFC 4180.
@@ -46,13 +45,10 @@ import java.util.TimeZone;
*/
public class CsvTrackWriter implements TrackFormatWriter {
static final NumberFormat SHORT_FORMAT = NumberFormat.getInstance();
static final SimpleDateFormat TIMESTAMP_FORMAT =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
private static final NumberFormat SHORT_FORMAT = NumberFormat.getInstance();
static {
SHORT_FORMAT.setMaximumFractionDigits(4);
TIMESTAMP_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private int segmentIdx = 0;
@@ -93,7 +89,7 @@ public class CsvTrackWriter implements TrackFormatWriter {
@Override
public void writeLocation(Location location) {
String timeStr = TIMESTAMP_FORMAT.format(new Date(location.getTime()));
String timeStr = FileUtils.FILE_TIMESTAMP_FORMAT.format(new Date(location.getTime()));
writeCommaSeparatedLine("P",
timeStr,
Double.toString(location.getLatitude()),
@@ -109,7 +105,7 @@ public class CsvTrackWriter implements TrackFormatWriter {
@Override
public void writeWaypoint(Waypoint waypoint) {
Location location = waypoint.getLocation();
String timeStr = TIMESTAMP_FORMAT.format(new Date(location.getTime()));
String timeStr = FileUtils.FILE_TIMESTAMP_FORMAT.format(new Date(location.getTime()));
writeCommaSeparatedLine("WAYPOINT",
timeStr,
Double.toString(location.getLatitude()),
@@ -18,6 +18,7 @@ package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import android.location.Location;
@@ -27,10 +28,8 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Log of one track.
@@ -38,11 +37,8 @@ import java.util.TimeZone;
* @author Sandor Dornbush
*/
public class GpxTrackWriter implements TrackFormatWriter {
private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private final NumberFormat elevationFormatter;
private final NumberFormat coordinateFormatter;
private final SimpleDateFormat timestampFormatter;
private PrintWriter pw = null;
private Track track;
@@ -57,9 +53,6 @@ public class GpxTrackWriter implements TrackFormatWriter {
coordinateFormatter.setMaximumFractionDigits(5);
coordinateFormatter.setMaximumIntegerDigits(3);
coordinateFormatter.setGroupingUsed(false);
timestampFormatter = new SimpleDateFormat(TIMESTAMP_FORMAT);
timestampFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private String formatLocation(Location l) {
@@ -144,7 +137,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
pw.println("<trkpt " + formatLocation(l) + ">");
Date d = new Date(l.getTime());
pw.println("<ele>" + elevationFormatter.format(l.getAltitude()) + "</ele>");
pw.println("<time>" + timestampFormatter.format(d) + "</time>");
pw.println("<time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(d) + "</time>");
pw.println("</trkpt>");
}
}
@@ -164,7 +157,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
if (l != null) {
pw.println("<wpt " + formatLocation(l) + ">");
pw.println("<ele>" + elevationFormatter.format(l.getAltitude()) + "</ele>");
pw.println("<time>" + timestampFormatter.format(l.getTime()) + "</time>");
pw.println("<time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(l.getTime()) + "</time>");
pw.println("<name>" + StringUtils.stringAsCData(waypoint.getName())
+ "</name>");
pw.println("<desc>"
@@ -21,6 +21,7 @@ import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.SystemUtils;
import android.content.Context;
@@ -30,10 +31,8 @@ import android.os.Build;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Write out a a track in the Garmin training center database, tcx format.
@@ -44,10 +43,9 @@ import java.util.TimeZone;
* Garmin Training Center 3.5.3.
*
* @author Sandor Dornbush
* @author Dominik Ršttsches
* @author Dominik Ršttsches
*/
public class TcxTrackWriter implements TrackFormatWriter {
protected static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
// These are the only sports allowed by the TCX v2 specification for fields
// of type Sport_t.
@@ -59,7 +57,6 @@ public class TcxTrackWriter implements TrackFormatWriter {
private static final String TCX_TYPE_RELEASE = "Release";
private static final String TCX_TYPE_INTERNAL = "Internal";
private final SimpleDateFormat timestampFormatter;
private final Context context;
private PrintWriter pw = null;
@@ -70,9 +67,6 @@ public class TcxTrackWriter implements TrackFormatWriter {
public TcxTrackWriter(Context context) {
this.context = context;
timestampFormatter = new SimpleDateFormat(TIMESTAMP_FORMAT);
timestampFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@SuppressWarnings("hiding")
@@ -117,7 +111,7 @@ public class TcxTrackWriter implements TrackFormatWriter {
return;
}
String startTime = timestampFormatter.format(track.getStatistics().getStartTime());
String startTime = FileUtils.FILE_TIMESTAMP_FORMAT.format(track.getStatistics().getStartTime());
pw.println(" <Activities>");
pw.format(" <Activity Sport=\"%s\">\n", categoryToTcxSport(track.getCategory()));
@@ -150,7 +144,7 @@ public class TcxTrackWriter implements TrackFormatWriter {
}
pw.println(" <Trackpoint>");
Date d = new Date(location.getTime());
pw.println(" <Time>" + timestampFormatter.format(d) + "</Time>");
pw.println(" <Time>" + FileUtils.FILE_TIMESTAMP_FORMAT.format(d) + "</Time>");
pw.println(" <Position>");
pw.print(" <LatitudeDegrees>");
@@ -21,7 +21,7 @@ import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
/**
@@ -30,8 +30,6 @@ import java.util.Date;
* @author Matthew Simmons
*/
class DefaultTrackNameFactory {
private static final String TIMESTAMP_DATE_FORMAT = "yyyy-MM-dd HH:mm";
private final Context context;
DefaultTrackNameFactory(Context context) {
@@ -48,7 +46,7 @@ class DefaultTrackNameFactory {
*/
String newTrackName(long trackId, long startTime) {
if (useTimestampTrackName()) {
SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMP_DATE_FORMAT);
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
return formatter.format(new Date(startTime));
} else {
return String.format(context.getString(R.string.track_name_format), trackId);
@@ -20,6 +20,8 @@ import com.google.android.apps.mytracks.Constants;
import android.os.Environment;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Pattern;
/**
@@ -39,6 +41,16 @@ public class FileUtils {
private static final Pattern PROHIBITED_CHAR_PATTERN =
Pattern.compile("[^ A-Za-z0-9_.()-]+");
/**
* Timestamp format in UTC time zone.
*/
public static final SimpleDateFormat FILE_TIMESTAMP_FORMAT =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
static {
FILE_TIMESTAMP_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
/**
* Builds a path inside the My Tracks directory in the SD card.
*
@@ -40,7 +40,8 @@ import junit.framework.TestCase;
*/
public class DocsHelper_AddTrackRowTest extends TestCase {
private static final long TIME = 1288721514000L;
private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
private static class StringWritingDocsHelper extends DocsHelper {
String writtenSheetUri = null;
@@ -55,12 +56,7 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
}
public void testAddTrackRow_imperial() throws Exception {
StringWritingDocsHelper docsHelper = new StringWritingDocsHelper() {
@Override
protected String getDisplayDate(Context context, long time) {
return DATE_FORMAT.format(new Date(TIME));
}
};
StringWritingDocsHelper docsHelper = new StringWritingDocsHelper();
addTrackRow(docsHelper, false);
String expectedData =
@@ -94,12 +90,7 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
}
public void testAddTrackRow_metric() throws Exception {
StringWritingDocsHelper docsHelper = new StringWritingDocsHelper() {
@Override
protected String getDisplayDate(Context context, long time) {
return DATE_FORMAT.format(new Date(TIME));
}
};
StringWritingDocsHelper docsHelper = new StringWritingDocsHelper();
addTrackRow(docsHelper, true);
// The imperial test verifies that the tags come out in the proper order,
@@ -3,13 +3,9 @@ package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.content.MyTracksLocation;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.io.file.TcxTrackWriter;
import com.google.android.apps.mytracks.io.file.TrackFormatWriter;
import com.google.android.apps.mytracks.util.FileUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -20,15 +16,6 @@ import org.w3c.dom.Element;
* @author Sandor Dornbush
*/
public class TcxTrackWriterTest extends TrackFormatWriterTest {
private DateFormat timestampFormatter;
@Override
public void setUp() throws Exception {
super.setUp();
timestampFormatter = new SimpleDateFormat(TcxTrackWriter.TIMESTAMP_FORMAT);
timestampFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
public void testXmlOutput() throws Exception {
TrackFormatWriter writer = new TcxTrackWriter(getContext());
@@ -72,7 +59,7 @@ public class TcxTrackWriterTest extends TrackFormatWriterTest {
assertEquals(Double.toString(loc.getLongitude()),
getChildTextValue(posTag, "LongitudeDegrees"));
assertEquals(timestampFormatter.format(loc.getTime()),
assertEquals(FileUtils.FILE_TIMESTAMP_FORMAT.format(loc.getTime()),
getChildTextValue(tag, "Time"));
assertEquals(Double.toString(loc.getAltitude()),
getChildTextValue(tag, "AltitudeMeters"));
@@ -18,7 +18,7 @@ package com.google.android.apps.mytracks.services;
import android.content.Context;
import android.test.AndroidTestCase;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
/**
@@ -52,7 +52,7 @@ public class DefaultTrackNameFactoryTest extends AndroidTestCase {
DefaultTrackNameFactory factory =
new MockDefaultTrackNameFactory(getContext(), true);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
assertEquals(formatter.format(new Date(TIMESTAMP)),
factory.newTrackName(1, TIMESTAMP));
}