mobile/android/base/background/common/DateUtils.java

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:c46701683cad
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.gecko.background.common;
6
7 import java.util.Calendar;
8 import java.util.Formatter;
9 import java.util.TimeZone;
10
11 public class DateUtils {
12 private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
13
14 public static final class DateFormatter {
15 private final Calendar calendar;
16 private final Formatter formatter;
17 private final StringBuilder builder;
18
19 public DateFormatter() {
20 this.calendar = Calendar.getInstance(UTC);
21 this.builder = new StringBuilder(); // So we can reset it.
22 this.formatter = new Formatter(this.builder, null);
23 }
24
25 public String getDateString(long time) {
26 calendar.setTimeInMillis(time);
27 builder.setLength(0);
28 return formatter.format("%04d-%02d-%02d",
29 calendar.get(Calendar.YEAR),
30 calendar.get(Calendar.MONTH) + 1, // 0-indexed.
31 calendar.get(Calendar.DAY_OF_MONTH))
32 .toString();
33 }
34
35 public String getDateStringForDay(long day) {
36 return getDateString(GlobalConstants.MILLISECONDS_PER_DAY * day);
37 }
38 }
39
40 public static int getDay(final long time) {
41 return (int) Math.floor(time / GlobalConstants.MILLISECONDS_PER_DAY);
42 }
43 }

mercurial