-1

I need to pass '2017-01-01 00:00:00' to URl but I cannot as browsers represents space character with %20 so I need to first convert 2017-01-01 00:00:00 to 2017-01-01%2000:00:00. I can do this easily with splits and other methods but I am confused if java provides any built-in functions.

UPDATE : thanks for valuable downvote and answers but URLEncoder.encode converts 2017-01-01 00:00:00 to 2017-01-01+00%3A00%3A00 and not 2017-01-01%2000:00:00 and the link provided by downvoters didnot help either. and I need to save this date on disk with 2017-01-01 00:00:00 and I don't want to decode it again and also on server there is strict regex check that only allows this specific formats and they didnt care to urldecode so my option is to use that give format i.e '2017-01-01%2000:00:00` and nothing else; Sure I can do simple replace to whitespace with %20 and everything will be alright,then again I asked if there is any java function that only does this conversion like if we can provide a format and it gives us back the formatted value. This might sound stupid then again I asked if this is possible.

Saurab
  • 1,931
  • 5
  • 20
  • 33

1 Answers1

1

You can use URLEncoder ...

String encoded = URLEncoder.encode("2017-01-01 00:00:00", "UTF-8")

Or alternatively use Apache HTTP Client's URIBuilder to build the complete URI, for example:

URIBuilder uriBuilder = new org.apache.http.client.utils.URIBuilder("http://host/endPoint");
uriBuilder.addParameter("parameterName", "2017-01-01 00:00:00");
String url = uriBuilder.toString();
glytching
  • 44,936
  • 9
  • 114
  • 120