Converting a String type to a Date
There is a String
entry 01:22:00
(hh. mm. ss). You need to make it type Date
. How can this be done?
Second: There is a String
record Sun Jan 24 23:59:20 SAMT 2016
. How do I make it Date
, but leave only the month and year?
6
Author: Александр Семпроний Гракх, 2016-01-24
2 answers
Code:
1)
String string = "01:22:00";
DateFormat format = new SimpleDateFormat("hh:mm:ss");
Date date;
date = format.parse(string);
System.out.println(date);
2)
String string = "Sun Jan 24 23:59:20 SAMT 2016";
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
Date date;
date = format.parse(string);
System.out.println(date);
You can read more in the Oracle documentation, i.e. here
9
Author: dirkgntly, 2016-08-29 09:41:39
private Date parseDate(String date, String format) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(date);
}
//Usage
Date date = parseDate("19/05/2009", "dd/MM/yyyy");
3
Author: carapuz, 2016-01-24 20:34:46