1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/common/DateUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,43 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.common; 1.9 + 1.10 +import java.util.Calendar; 1.11 +import java.util.Formatter; 1.12 +import java.util.TimeZone; 1.13 + 1.14 +public class DateUtils { 1.15 + private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); 1.16 + 1.17 + public static final class DateFormatter { 1.18 + private final Calendar calendar; 1.19 + private final Formatter formatter; 1.20 + private final StringBuilder builder; 1.21 + 1.22 + public DateFormatter() { 1.23 + this.calendar = Calendar.getInstance(UTC); 1.24 + this.builder = new StringBuilder(); // So we can reset it. 1.25 + this.formatter = new Formatter(this.builder, null); 1.26 + } 1.27 + 1.28 + public String getDateString(long time) { 1.29 + calendar.setTimeInMillis(time); 1.30 + builder.setLength(0); 1.31 + return formatter.format("%04d-%02d-%02d", 1.32 + calendar.get(Calendar.YEAR), 1.33 + calendar.get(Calendar.MONTH) + 1, // 0-indexed. 1.34 + calendar.get(Calendar.DAY_OF_MONTH)) 1.35 + .toString(); 1.36 + } 1.37 + 1.38 + public String getDateStringForDay(long day) { 1.39 + return getDateString(GlobalConstants.MILLISECONDS_PER_DAY * day); 1.40 + } 1.41 + } 1.42 + 1.43 + public static int getDay(final long time) { 1.44 + return (int) Math.floor(time / GlobalConstants.MILLISECONDS_PER_DAY); 1.45 + } 1.46 +}