I kind of stuck on this one. Basically, we have a string that is actually a date with a format YYYYMM. For example, 201901.

I wanted to convert this into a date record, but I found out that we could not set a particular date format in the method 'format()'.

Is there any way I could create a date record with date format YYYYMM?



You can use the method newInstance to achieve this.

Date myDate = date.newinstance(1960, 2, 17); //YYYY, MM, DD

Just split the string (You can use substring(startIndex, endIndex) ) and pass in the method. For the day you may need to pass a static value(like 1).

  • 1
    Oh yeah. Now I understand. Thanks for the suggestion of having the date as just a static value. I actually forgot about that one, that's why I didn't use newinstance. Yes, I think this would work for me. – Jerard Dela Victoria Jan 29 '19 at 13:26
  • @JerardDelaVictoria If this solves your problem then you can accept the answer and close this question. – Tushar Sharma Jan 30 '19 at 7:31
  • sorry for the late reply. yup. I coded this and it works! Thanks for the help. – Jerard Dela Victoria Feb 1 '19 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.