Java Hour Format formatTime(String dateTimeString)

Here you can find the source of formatTime(String dateTimeString)

Description

Extracts hour and minutes from a dateDimestring

License

Apache License

Parameter

Parameter Description
String in format 2012-10-15T08:17:00

Return

String in format HH:MM

Declaration

public static String formatTime(String dateTimeString) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//from w w w .  ja  va 2 s. c o  m
     *Extracts hour and minutes from a dateDimestring
     * @param String in format 2012-10-15T08:17:00
     * @return String in format HH:MM
     * */
    public static String formatTime(String dateTimeString) {
        String formattedTime = "";
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(dateTimeString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        String hour;
        if (cal.get(Calendar.HOUR_OF_DAY) < 10) {
            hour = "0" + String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        } else {
            hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        }
        String minute;
        if (cal.get(Calendar.MINUTE) < 10) {
            minute = "0" + String.valueOf(cal.get(Calendar.MINUTE));
        } else {
            minute = String.valueOf(cal.get(Calendar.MINUTE));
        }

        formattedTime = formattedTime + " " + hour + ":" + minute;
        return hour + ":" + minute;
    }
}

Related

  1. formatTime(long ms)
  2. formatTime(long time)
  3. formatTime(long time)
  4. formatTime(long time)
  5. formatTime(long time, TimeUnit unit)
  6. formatTime_(String time)
  7. formatTime_1(long time_value)
  8. formatTime_2(long time_value, long frame_rate)
  9. formatToHour(Date date)