Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract time from date String

How can I format the "2010-07-14 09:00:02" date string to depict just "9:00"?

like image 429
JJunior Avatar asked Aug 17 '10 17:08

JJunior


People also ask

How do I extract time from text in Excel?

1. Select a blank cell, and type this formula =TIME(HOUR(A1),MINUTE(A1), SECOND(A1)) (A1 is the first cell of the list you want to extract time from), press Enter button and drag the fill handle to fill range. Then only time text has been eatraced from the list.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


2 Answers

Use SimpleDateFormat to convert between a date string and a real Date object. with a Date as starting point, you can easily apply formatting based on various patterns as definied in the javadoc of the SimpleDateFormat (click the blue code link for the Javadoc).

Here's a kickoff example:

String originalString = "2010-07-14 09:00:02"; Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(originalString); String newString = new SimpleDateFormat("H:mm").format(date); // 9:00 
like image 59
BalusC Avatar answered Sep 30 '22 15:09

BalusC


Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2010-07-14 09:00:02"); String time = new SimpleDateFormat("H:mm").format(date); 

http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

like image 44
Adam Avatar answered Sep 30 '22 16:09

Adam