Hi,
I have a date in yyyy-mm-dd format. How to convert string to date in java in yyyy-mm-dd format?
Thanks
Hi,
You can use the SimpleDateFormat class to format string to date.
Here is the full example code:
package net.roseindia; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /* * Example of formatting date in Java * from yyyy-mm-dd formaat */ public class StringDateFormat { public static void main(String[] args) { String dt = "2017-05-08"; String pattern = "yyyy-mm-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Date date = null; try { date = simpleDateFormat.parse(dt); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Date:" + date); } }
Thanks
Hi,
Small update: Use the MM in format, MM is for month. The small (mm) is for minute and you will get wrong results. Current format to be used is:
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Thanks
Hi,
Here is the output of the program in Eclipse IDE:
Thanks
HI,
If you want to learn Java date conversion in detail then check following tutorials:
Thanks
Ads