I getting date from MySql, in the format of "yyyy-mm-dd hh:mm:ss" I just want to split the date and time, show date separately and time also. Time like hh:mm, date like yyyy-mm-dd.
-
Use SimpleDateFormatter – Triode Apr 16 '13 at 08:47
-
http://developer.android.com/reference/java/text/SimpleDateFormat.html – Nermeen Apr 16 '13 at 08:48
-
1you'd rather get the date as a long, if you have the hand on the mysql part. – njzk2 Apr 16 '13 at 08:49
-
you don't have a timezone ? – njzk2 Apr 16 '13 at 08:53
-
possible duplicate of [Android - date format conversion](http://stackoverflow.com/questions/3778202/android-date-format-conversion) – lostInTransit Apr 17 '13 at 09:23
8 Answers
You Can also use StringTokenizer class, it allows you to break string into tokens. You can specify characters that will separate tokens.
Example:
String date = "yyyy-mm-dd hh:mm:ss";
StringTokenizer tk = new StringTokenizer(date);
String date = tk.nextToken(); // <--- yyyy-mm-dd
String time = tk.nextToken(); // <--- hh:mm:ss

- 2,836
- 5
- 30
- 44
It depends on how you will me using it, but you can do a String.split(" ")
to get both parts or parse it as a Date/Calendar and extract the date and time.
For example:
String d = "2013-05-11 13:59:50";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
try {
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(d));
Log.d("DEBUG", "year:" + c.get(Calendar.YEAR));
//...
} catch (ParseException e) {
}
Also, as njzk2 said: other way would be to store a long value in your table so you can extract it directly to a Date/calendar
Calendar c = Calendar.getInstance();
c.setTimeInMillis(yourDbLongValue);

- 1,037
- 12
- 19
-
-
Yep, thats what i meant with the Date/Calendar stuff. I updated my answer. – Aballano Apr 16 '13 at 08:52
Try following code.
String dateValue = "2013-05-11 13:59:50";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = null;
try {
date = sdf.parse(dateValue);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm");
String sa = timeFormatter.format(date);

- 6,802
- 8
- 36
- 54
If the results from MySQL are controllable, you can do select DATE(my_date_time) FROM my_table;
this will give you only the date. If not, then I guess if you look at the date as a string you can substring (Split) it and get the first 10 characters.

- 6,752
- 2
- 30
- 43
use the substring method
startdate=start_time.substring(0, 10);
starttime=start_time.substring(11, 16);

- 29,301
- 6
- 52
- 87

- 163
- 9
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fullDate = format.format(mDate.getTime());
date = fullDate.substring(0, fullDate.indexOf(" ");
time = fullDate.substring(fullDate.indexOf(" " + 1);

- 15,883
- 5
- 59
- 72
RoadBean roadBean = new RoadBean();
StringTokenizer tk = new StringTokenizer(jsonObject.getString("datetime"));
String split_date = tk.nextToken();
String split_time = tk.nextToken();
roadBean.setDate(split_date);
roadBean.setTime(split_time);
mRoadBeanList.add(roadBean);

- 695
- 7
- 24
Split time as hour, minute and second.
String timeSpent="12:30:10";
String[] seperatedTime= timeSpent.split(":");
int hours = Integer.parseInt(seperatedTime[0]);
int minutes = Integer.parseInt(seperatedTime[1]);
int seconds = Integer.parseInt(seperatedTime[2]);
int duration = 3600 * hours + 60 * minutes + seconds;

- 8,950
- 115
- 65
- 78

- 320
- 4
- 7