devxlogo

Converting a Date to a Certain String Format

Converting a Date to a Certain String Format

The format() method from SimpleDateFormat class helps to convert a Date object into certain format String objects by using the supplied pattern string.

Date today = new Date();SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); //pattern is specified here System.out.println(dateFormat.format(today)); //30-Aug-18 Patterns can be applied again by using applyPattern() dateFormat.applyPattern("dd-MM-yyyy"); System.out.println(dateFormat.format(today)); //30-08-2018 dateFormat.applyPattern("dd-MM-yyyy HH:mm:ss E"); System.out.println(dateFormat.format(today)); //30-08-2018 16:26:41 Thu

devx-admin

Share the Post: