Convert Long date to specific short date format - date

Convert Long date format to specific short date format.
I want to get the Date from Datepicker(Jcalander) , format to dd-mm-yyyy format and assign to String variable. I tried using codes shown below. But didnt get the date format i want.
SimpleDateFormat simpleFormat = (SimpleDateFormat) jCalendarCombo1.getDateFormat();
Date date = jCalendarCombo1.getDate();
System.out.println(date); // Prints Thu Mar 28 00:00:00 IST 2013
String s = simpleFormat.format(date);
System.out.println(s); // prints Thursday, March 28, 2013
System.out.println("Date SHORT format: " + DateFormat.getDateInstance(DateFormat.SHORT).format(date)); // prints 3/28/13

If you want a fixed, non locale dependent format, you can just create it yourself;
SimpleDateFormat shortformat = new SimpleDateFormat("dd-MM-yyyy");
String s = shortformat.format(date);
System.out.println(s); // Prints 29-03-2013

Related

Revert datetime format, from short to long

Tried searching around couldn't really find anything. Was hoping to find a way to revert the datetime format.
So I start off with: 4-11-22 and I want to change to Friday, 12 November 2022.
Using the intl package
import intl package
import 'package:intl/intl.dart';
Then, you can do as follows:
String myDate = '4-11-22'; // input date
String pattern = 'dd-MM-yy'; // define parse pattern for the input date
DateTime date = DateFormat(pattern).parse(myDate); // parse the input date
String newPattern = 'EEEE, dd MMMM yyyy'; // define new pattern
String formattedDate = DateFormat(newPattern).format(date); // reformat
print(formattedDate); // result: Friday, 04 November 2022
Try on DartPad
For more formatting possibilities, go to the docs.
final oldDateDateTime = DateFormat('dd-MM-yy').parse('4-11-22');
final newDateString = DateFormat('EEEE, d MMMM y', 'en_US').format(oldDateDateTime);
print(oldDateDateTime.toString());
print(newDateString);
Output:
2022-11-04 00:00:00.000
Friday, 4 November 2022
More in: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
You can do as follows
final DateFormat formatter = DateFormat('EEEE, dd MMMM yyyy');
final String formatted = formatter.format(DateTime.now());
Then you use the formatted

Convert time AM and PM TO 24 hour format

String timer = "01:30 PM"
String = "12/10/2020"
DateTime fdate = DateFormat("yyyy-MM-dd").parse(dates);
DateTime ftime = DateFormat("hh:mm:ss").parse(timer);
I am getting the error while converting the string time into the Original time format. How to convert that time and date into the different format.
How to get the combination of date and time like 2020-10-12 13:30:00
Change ftime to :
DateTime ftime = DateFormat("HH:mm:ss").parse(timer);
Consider reading DateFormat Documentation for more information

Issue saving a string as ISO date format

I'm trying to save a date to MongoDB from FullCalendar in my Grails application.
I'm trying to parse the string 2015-12-27T00:00:00.000Z into the below format:
def startDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.start)
def endDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.end)
But, weirdly when I print the formatted date, I get Sun Dec 28 05:30:00 IST 2014. I don't know what or how that particular date is picked.
You should use lowercase y for year. Uppercase Y is for "Week year".
new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "2015-12-27T00:00:00.000Z")
===> Sat Dec 26 19:00:00 EST 2015
import java.text.SimpleDateFormat;
println new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX").parse("2018-07-30 09:57:15 +0800")

JavaFX8 Convert String to date using DateTimeStringConverter

I would like convert string to date (dd/MM/YYYY) - this is useful for compare dates in TableColumns.
so i use DateTimeStringConverter (the string "01/11/2014" is a value of DatePicker)
DateTimeStringConverter format = new DateTimeStringConverter(Locale.FRANCE,
"dd/MM/YYYY");
Date d1 = format.fromString("01/11/2014");
d1.toString()
I don't obtain the right date but this date = "Mon Dec 30 00:00:00 CET 2013" !!!
I don't understand what is the problem (which in fact should not be a problem) ?
Any ideas ?
Thank you in advance
Fabrice

Groovy Date code producing unexpected output

Why is below code producing output like this ?
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-mm-dd hh:mm:ss', oldDate )
Output : Sat Jan 05 01:34:54 EST 2013
When it should simply produce December 5 as the date in the output ? In fact in the original string whatever month I put in it produces the exact same output.
The problem is your date format isn't correct. mm is for minutes where MM is for month. So it should be:
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-MM-dd hh:mm:ss', oldDate )
You can find out more from the Java API docs for SimpleDateFormat.