3

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.

Android learner
  • 1,871
  • 4
  • 24
  • 36

8 Answers8

9

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
Jilberta
  • 2,836
  • 5
  • 30
  • 44
2

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);
Aballano
  • 1,037
  • 12
  • 19
1

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);
Dev.Sinto
  • 6,802
  • 8
  • 36
  • 54
0

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.

g00dy
  • 6,752
  • 2
  • 30
  • 43
0

use the substring method

startdate=start_time.substring(0, 10); 
starttime=start_time.substring(11, 16);
RandomSeed
  • 29,301
  • 6
  • 52
  • 87
srinivas
  • 163
  • 9
0
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);
Nermeen
  • 15,883
  • 5
  • 59
  • 72
0

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);

Ashwin H
  • 695
  • 7
  • 24
0

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;
slfan
  • 8,950
  • 115
  • 65
  • 78
Rahul Singh
  • 320
  • 4
  • 7