Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut and replace String

Tags:

java

I have a following string which I have to split and replace with "/" on some condition**

String date = "20131105";

I want to change these string to "2013/11/05"

Edit:

I mean variable date must be String not Date data type

like image 263
Rex Avatar asked Sep 12 '26 06:09

Rex


2 Answers

Do like this

Date date = new SimpleDateFormat("yyyyMMdd").parse("20131105");
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);

Output

2013/11/05
like image 62
Prabhakaran Ramaswamy Avatar answered Sep 14 '26 19:09

Prabhakaran Ramaswamy


Use the substring method.

date = date.substring(0, 4) + "/" + date.substring(4, 6) + "/" + date.substring(6, 8);
like image 36
Sorter Avatar answered Sep 14 '26 21:09

Sorter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!