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));
mdakin
source share