Search code examples
javaclock

Get time as year/month/day/hour/minutes and convert to millisecond like getTime() in java or android


I want to get time from user(with editbox like year/month/day/hour/minutes). How can i convert it to millisecond like getTime function or convert to minutes like getTime()/60000?


Solution

  • You can use a DatePicker and then use the getters from that to either make a Calendar object or to get the time in millis.

    I use this function in my projects to get the Calendar from the DatePicker, you may want to extend it to set the hours/ minutes as well:

    public static Calendar getCalenderFromDatePicker(DatePicker dp) {
        int day = dp.getDayOfMonth();
        int month = dp.getMonth();
        int year = dp.getYear();
    
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return calendar;
    }
    

    Once you have the Calendar you can use myCalendar.getTimeInMillis() which returns a long of the time in millis since the epoch.