get Now Time - Android java.util

Android examples for java.util:Time

Description

get Now Time

Demo Code


//package com.java2s;

import java.util.HashMap;

public class Main {
    public static final String YEAR_KEY = "year";
    public static final String MONTH_KEY = "month";
    public static final String DAY_KEY = "day";
    public static final String HOUR_KEY = "hour";
    public static final String MINUTE_KEY = "minute";
    public static final String SECOND_KEY = "second";

    public static HashMap<String, Object> getNowTime() {
        HashMap<String, Object> result = new HashMap<String, Object>();

        String strDate = new java.text.SimpleDateFormat("yyyyMMdd")
                .format(new java.util.Date());
        String strTime = new java.text.SimpleDateFormat("HHmmss")
                .format(new java.util.Date());

        int year = Integer.parseInt(strDate.substring(0, 4));
        int month = Integer.parseInt(strDate.substring(4, 6));
        int day = Integer.parseInt(strDate.substring(6, 8));

        int hour = Integer.parseInt(strTime.substring(0, 2));
        int minute = Integer.parseInt(strTime.substring(2, 4));
        int second = Integer.parseInt(strTime.substring(4, 6));

        result.put(YEAR_KEY, year);//  w  ww. jav a  2 s .  co  m
        result.put(MONTH_KEY, month);
        result.put(DAY_KEY, day);
        result.put(HOUR_KEY, hour);
        result.put(MINUTE_KEY, minute);
        result.put(SECOND_KEY, second);

        return result;
    }
}

Related Tutorials