I'd like to convert a date in date1 format to a date object in date2 format.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy"); SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.set(2012, 8, 21); Date date = cal.getTime(); Date date1 = simpleDateFormat.parse(date); Date date2 = simpleDateFormat.parse(date1); println date1 println date2
Using java. The LocalDate class represents a date-only value without time-of-day and without time zone. String input = "January 08, 2017"; Locale l = Locale.US ; DateTimeFormatter f = DateTimeFormatter. ofPattern( "MMMM dd, uuuu" , l ); LocalDate ld = LocalDate. parse( input , f );
You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);
Use SimpleDateFormat#format
:
DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH); DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd"); Date date = originalFormat.parse("August 21, 2012"); String formattedDate = targetFormat.format(date); // 20120821
Also note that parse
takes a String
, not a Date
object, which is already parsed.
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