0

I have some string respone date like this:

2018-11-30 12:00:00

and i want just get the time, so i want like this:

12.00

and my question, can we delete some character like that?

Uray Febri
  • 373
  • 4
  • 16

5 Answers5

2

Sounds like you want to use SimpleDateFormat so something like:

Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String folderName = formatter.format(today);

Based on your comment try:

int index = time.indexOf(‘:’);
int start = index -2;
int end = index + 2;
String newTime = time.substring(start,end);
Keheira
  • 90
  • 1
  • 10
1

You can try this

Calendar calendar = Calendar.getInstance();
        SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
        String strDate = "Current Time : " + mdformat.format(calendar.getTime());
        display(strDate);
Leon Zaii
  • 163
  • 13
  • thank you for respone, but how replace the time when String value from api is 2018-11-30 12:00:00 and i want to just get the time like 12.00 – Uray Febri Oct 04 '18 at 04:02
  • You just need to format it. [Here might have answers](https://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – Leon Zaii Oct 04 '18 at 04:05
1

You can use SimpleDateFormat to get time and then replace : by . I hope it can help your problem!

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1
String apiDateString = "2018-11-30 12:00:00"; // in this case, your string from api
Date apiDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(apiDateString); // convert api date string to date
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String yourDateString = formatter.format(apiDate);

I think you want this.

Edited with little comment.

  1. convert string to date. (apiDateString to apiDate)
  2. create new format for what you want to form. In this case, 12.00
  3. convert apiDate to your format. (apiDate -> 12.00)
Wooyoung Tyler Kim
  • 484
  • 2
  • 9
  • 27
0

You can refer to this post. Use SimpleDateFormat to format you date.

From String to Date

// Convert from string to date
String yourDate = "2018-11-30 12:00:00";  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(yourDate);

//Use calender instead of date to getHours and minute, due to deprecated
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.get(Calendar.HOUR);
calendar.get(Calendar.MINUTE);

System.out.println(calendar.get(Calendar.HOUR_OF_DAY)+"."+calendar.get(Calendar.MINUTE)); //12.0
Jyyff
  • 1
  • 1