michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.background.common; michael@0: michael@0: import java.util.Calendar; michael@0: import java.util.Formatter; michael@0: import java.util.TimeZone; michael@0: michael@0: public class DateUtils { michael@0: private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); michael@0: michael@0: public static final class DateFormatter { michael@0: private final Calendar calendar; michael@0: private final Formatter formatter; michael@0: private final StringBuilder builder; michael@0: michael@0: public DateFormatter() { michael@0: this.calendar = Calendar.getInstance(UTC); michael@0: this.builder = new StringBuilder(); // So we can reset it. michael@0: this.formatter = new Formatter(this.builder, null); michael@0: } michael@0: michael@0: public String getDateString(long time) { michael@0: calendar.setTimeInMillis(time); michael@0: builder.setLength(0); michael@0: return formatter.format("%04d-%02d-%02d", michael@0: calendar.get(Calendar.YEAR), michael@0: calendar.get(Calendar.MONTH) + 1, // 0-indexed. michael@0: calendar.get(Calendar.DAY_OF_MONTH)) michael@0: .toString(); michael@0: } michael@0: michael@0: public String getDateStringForDay(long day) { michael@0: return getDateString(GlobalConstants.MILLISECONDS_PER_DAY * day); michael@0: } michael@0: } michael@0: michael@0: public static int getDay(final long time) { michael@0: return (int) Math.floor(time / GlobalConstants.MILLISECONDS_PER_DAY); michael@0: } michael@0: }