I've got GregorianCalendar instances and need to use SimpleDateFormat (or maybe something that can be used with calendar but that provides required #fromat() feature) to get needed output. Please, suggest work arounds as good as permanent solutions.
Creating SimpleDateFormat instance String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); In the example above the String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “MM-dd-yyyy”.
DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.
Day/Month/Year – DD/MM/YYYY – Mainly used in European languages, as well as the United Nations when writing the full date format in official documents. It's also used in India, Australia, and most of Africa and Latin America. Year/Month/Day – YYYY/MM/DD – Mostly found in the Chinese language.
Try this:
Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); dateFormat.setTimeZone(cal.getTimeZone()); System.out.println(dateFormat.format(cal.getTime()));
eQui's answer is missing a step
Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); #---- This uses the provided calendar for the output ----- dateFormat.setCalendar(cal); System.out.println(dateFormat.format(cal.getTime()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With