aboutsummaryrefslogtreecommitdiff
path: root/java/util
diff options
context:
space:
mode:
authorFeng Xiao <xfxyjwf@gmail.com>2017-03-08 16:56:43 -0800
committerFeng Xiao <xfxyjwf@gmail.com>2017-03-08 16:56:43 -0800
commit4ae8656b6dca8ad788efa2f8883f421676e62d8c (patch)
tree7f6be8206448d313a9c335ffc1234e2d1374c313 /java/util
parentfa1788026cf7c7310f068dbee7fbf71958ec1290 (diff)
downloadprotobuf-4ae8656b6dca8ad788efa2f8883f421676e62d8c.tar.gz
protobuf-4ae8656b6dca8ad788efa2f8883f421676e62d8c.tar.bz2
protobuf-4ae8656b6dca8ad788efa2f8883f421676e62d8c.zip
Make JsonFormat locale independent.
Diffstat (limited to 'java/util')
-rw-r--r--java/util/src/main/java/com/google/protobuf/util/Timestamps.java9
-rw-r--r--java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java6
2 files changed, 11 insertions, 4 deletions
diff --git a/java/util/src/main/java/com/google/protobuf/util/Timestamps.java b/java/util/src/main/java/com/google/protobuf/util/Timestamps.java
index 2160e4d5..1d631a2c 100644
--- a/java/util/src/main/java/com/google/protobuf/util/Timestamps.java
+++ b/java/util/src/main/java/com/google/protobuf/util/Timestamps.java
@@ -44,6 +44,7 @@ import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
+import java.util.Locale;
import java.util.TimeZone;
/**
@@ -83,7 +84,7 @@ public final class Timestamps {
};
private static SimpleDateFormat createTimestampFormat() {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
// We use Proleptic Gregorian Calendar (i.e., Gregorian calendar extends
// backwards to year one) for timestamp formating.
@@ -386,11 +387,11 @@ public final class Timestamps {
static String formatNanos(int nanos) {
// Determine whether to use 3, 6, or 9 digits for the nano part.
if (nanos % NANOS_PER_MILLISECOND == 0) {
- return String.format("%1$03d", nanos / NANOS_PER_MILLISECOND);
+ return String.format(Locale.ENGLISH, "%1$03d", nanos / NANOS_PER_MILLISECOND);
} else if (nanos % NANOS_PER_MICROSECOND == 0) {
- return String.format("%1$06d", nanos / NANOS_PER_MICROSECOND);
+ return String.format(Locale.ENGLISH, "%1$06d", nanos / NANOS_PER_MICROSECOND);
} else {
- return String.format("%1$09d", nanos);
+ return String.format(Locale.ENGLISH, "%1$09d", nanos);
}
}
}
diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
index dafd9bb5..883706c1 100644
--- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
+++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
@@ -65,10 +65,16 @@ import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
import junit.framework.TestCase;
public class JsonFormatTest extends TestCase {
+ public JsonFormatTest() {
+ // Test that locale does not affect JsonFormat.
+ Locale.setDefault(Locale.forLanguageTag("hi-IN"));
+ }
+
private void setAllFields(TestAllTypes.Builder builder) {
builder.setOptionalInt32(1234);
builder.setOptionalInt64(1234567890123456789L);