1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.locationtracker.data;
18
19import java.util.Calendar;
20import java.util.TimeZone;
21
22/**
23 * Provides formatting date as string utilities
24 */
25public class DateUtils {
26
27    private DateUtils() {
28
29    }
30
31    /**
32     * Returns timestamp given by param in KML format ie yyyy-mm-ddThh:mm:ssZ,
33     * where T is the separator between the date and the time and the time zone
34     * is Z (for UTC)
35     *
36     * @return KML timestamp as String
37     */
38    public static String getKMLTimestamp(long when) {
39        TimeZone tz = TimeZone.getTimeZone("GMT");
40        Calendar c = Calendar.getInstance(tz);
41        c.setTimeInMillis(when);
42        return String.format("%tY-%tm-%tdT%tH:%tM:%tSZ", c, c, c, c, c, c);
43    }
44
45    /**
46     * Helper version of getKMLTimestamp, that returns timestamp for current
47     * time
48     */
49    public static String getCurrentKMLTimestamp() {
50        return getKMLTimestamp(System.currentTimeMillis());
51    }
52
53    /**
54     * Returns timestamp in following format: yyyy-mm-dd-hh-mm-ss
55     */
56    public static String getCurrentTimestamp() {
57        Calendar c = Calendar.getInstance();
58        c.setTimeInMillis(System.currentTimeMillis());
59        return String.format("%tY-%tm-%td-%tH-%tM-%tS", c, c, c, c, c, c);
60    }
61}
62