Here you can find the source of formatTime(String dateTimeString)
Parameter | Description |
---|---|
String | in format 2012-10-15T08:17:00 |
public static String formatTime(String dateTimeString)
//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; } }