How to separate date and time from datetime string?

I have a line like 8/29/2011 11:16:12 AM . I want to split this line into separate variables such as

 date = 8/29/2011 time = 11:16:12 AM 

Is it possible? If so, how would I do it?

+10
java android split datetime
source share
7 answers

One way to do this is to parse the date object and reformat it again:

  try { DateFormat f = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); Date d = f.parse("8/29/2011 11:16:12 AM"); DateFormat date = new SimpleDateFormat("MM/dd/yyyy"); DateFormat time = new SimpleDateFormat("hh:mm:ss a"); System.out.println("Date: " + date.format(d)); System.out.println("Time: " + time.format(d)); } catch (ParseException e) { e.printStackTrace(); } 

If you just want to cut it into pieces of date and time, just use the split to get the pieces

  String date = "8/29/2011 11:16:12 AM"; String[] parts = date.split(" "); System.out.println("Date: " + parts[0]); System.out.println("Time: " + parts[1] + " " + parts[2]); 

or

  String date = "8/29/2011 11:16:12 AM"; System.out.println("Date: " + date.substring(0, date.indexOf(' '))); System.out.println("Time: " + date.substring(date.indexOf(' ') + 1)); 
+25
source share

Try this:

 String main = "8/29/2011 11:16:12 AM"; String[] tmp; String date, time; tmp = main.split(" "); // [0] = 8/29/2011 // [1] = 11:16:12 // [2] = AM date = tmp[0].toString(); // 8/29/2011 time = tmp[1].toString() + " " + tmp[2].toString(); // 11:16:12 AM 
+1
source share

Try the split function in Java:

 String my_string="8/29/2011 11:16:12 AM" String [] my_date_time = null; my_date_time = my_string.split(" "); String Date_str=my_date_time[0]; String Time_str=my_date_time[1]+" "+my_date_time[2]; 

You get string variables like Date_str="8/29/2011" and Time_str="11:16:12 AM"

+1
source share
 String str = "8/29/2011 11:16:12 AM" String date = str.subString(0, str.indexOf(' ')); String time = str.subString(str.indexOf(' ')+1); 
+1
source share

Why not use DateFormat?

Check out http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

 String str = "8/29/2011 11:16:12 AM"; String fmt = "MM/dd/yyyy HH:mm:ss a"; DateFormat df = new SimpleDateFormat(fmt); Date dt = df.parse(str); DateFormat tdf = new SimpleDateFormat("HH:mm:ss a"); DateFormat dfmt = new SimpleDateFormat("MM/dd/yyyy"); String timeOnly = tdf.format(dt); String dateOnly = dfmt.format(dt); 

This gives more work / code, but this is the right way to do it.

+1
source share

You can do this by splitting your string into two substrings, as follows

 String main = "8/29/2011 11:16:12 AM"; String s[] = main.split(" ",2); String date = s[0]; String time = s[1]; 

NOTE The split method splits the string into two parts, as indicated in the second argument.

0
source share
 mDate = new Date(); DateFormat timeFormat = SimpleDateFormat.getTimeInstance(); DateFormat dateFormat = SimpleDateFormat.getDateInstance(); timeFormat.format(mDate); // formats to: 4:53:03 AM dateFormat.format(mDate); // formats to: Oct 16, 2014 
0
source share

All Articles