String value="2017/3/4"; I want my string value in LocalDate format like "2017-03-04" How to convert this value to my localdate without using any formatter and string methods like split(),indexof().
-
1That’s unclear. Can we use `String.length()`, `String.charAt()` and `String.toCharArray()`? Maybe even `String.replace()` or `String.replaceAll()`? Is `LocalDate.parse(CharSequence)` allowed (its implementation uses a formatter)? I’m afraid we need to have the constraints spelled out precisely. – Ole V.V. Jul 22 '17 at 07:31
-
What have you tried? You will learn more yourself and at the same time get better help if you tell us your attempts and how they were insufficient or failed or precisely where you got stuck. – Ole V.V. Jul 22 '17 at 07:33
-
1If you can't use **any** method from `String`, you won't be able to even **get** its value - and you need at least to get it to start doing anything. If you specify which methods can be used, provide more context (why you have such restrictions, or what exactly are you trying to solve) and ideally a [mcve], this increases the chances of the question to be better understood by everyone and find an answer. – Jul 22 '17 at 11:42
-
1don't use any string methods.Is there any possible ways to convert without formatter? – Tamil.S Jul 22 '17 at 16:50
-
input = 2017/5/22 Output = 2017-05-22. This is am try to say – Tamil.S Jul 22 '17 at 16:52
-
I added a few lines to my answer after your comments. @Tamil.S – Ole V.V. Jul 22 '17 at 19:04
-
If you can't use any `String` methods nor a formatter, sorry, there's no way. Even to **get** the value from the `String` you'll need to call some method. Any other approach (like using regex or reflection) will indirectly use some method. Java, in the end, is about calling other objects methods (not technically accurate, but still true) – Jul 22 '17 at 21:35
1 Answers
Easy way: Use String.replace(char oldChar, char newChar)
to replace the slashes with hyphens. Remember that replace
does not alter your string but instead returns a new string with the changes. Then use LocalDate.parse(CharSequence text)
to parse the new string into a LocalDate
(a String
is a CharSequence
, so this works).
If you are not allowed to use String.replace()
, use String.toCharArray()
to convert into a char[]
. Iterate through this array and replace each slash with a hyphen. Then build a new string through the String(char[] value)
constructor. Then proceed as above. Alternatively create a StringBuilder
from your original string using the StringBuilder(CharSequence seq)
constructor, iterate through it and replace each slash with a hyphen. Then you can simply pass the StringBuilder
into LocalDate.parse()
since this is also a CharSequence
. Edit: Please note that this last approach does not directly use any String
method at all, so maybe this was what was intended (the StringBuilder
constructor indirectly uses String.length()
and String.getChars()
).
If you are not allowed to use LocalDate.parse()
either since it implicitly is using a DateTimeFormatter
, then you need to extract the date, month and year from the string yourself. This is a bit more complicated. If this is necessary and you need help with it, let us know and I will explain.
As @Hugo said, if you are not allowed to use any String
methods nor any formatter, neither directly nor indirectly, then there is no way.