Groovy Date code producing unexpected output - date

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.

Related

Flutter/Dart : Parsing 'yyyy-MM-dd HH:MM:SS' producing wrong output?

I am parsing a date in format yyyy-MM-dd HH:MM:SS using DateFormat
But I am getting some other date in parsed output
final dt = '2022-02-07 05:00:11';
final datm = DateFormat('yyyy-MM-dd HH:MM:SS').parse(dt);
print ("DATE_CALC ${datm.day} - ${datm.month} - ${datm.year} ${datm.hour} : ${datm.minute} : ${datm.second} ");
Actual Output:
DATE_CALC 7 - 2 - 2021 5 : 0 : 0
Expected :
DATE_CALC 07 - 12 - 2022 05 : 00 : 11
Why I am getting some wrong date ? Am I doing anything wrong ?
MM is not month and minutes.... mm or MM :-)
wrong 'yyyy-MM-dd HH:MM:SS'
correct 'yyyy-MM-dd HH:mm:ss'
final dt = '2022-02-07 05:00:11';
final datm = DateFormat("yyyy-MM-dd HH:mm:ss").parse(dt);
print ("DATE_CALC ${datm.day} - ${datm.month} - ${datm.year} ${datm.hour} : ${datm.minute} : ${datm.second} ");
Try this
Hour minute and seconds can be parsed like this HH:mm:ss not like HH:MM:SS
Your formate is wrong
yyyy-MM-dd HH:MM:SS replace to 'yyyy-MM-dd HH:mm:ss'
it should be final datm = DateFormat('yyyy-MM-dd HH:mm:ss').parse(dt);

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

how to compare two custom dates in powershell v2.0

Is it possible to compare 2 custom dates. Am trying check if variables hold date1 is lessthan date2, if so, report saying date1 is older date.
I getting both dates from a. date1 from log file and date2 from application itself
now, both date1 and date2 are in required format ie,
$Date1 = Tue,Aug 16, 2016 12:40:03
$Date2 = Mon,Aug 22, 2016 16:33:02
my next step is compare these 2 dates and report if date1 is older date compare to Date2, which I don't know how to proceed.. Any help/ideas is much appreciated.
Thanks to Pete and Ansgar Wiechers
updated working Code :
$Date1DateTime = [DateTime]::ParseExact($Date1,'ddd,MMM d, yyyy, HH:mm:ss',[Globalization.CultureInfo]::InvariantCulture); $Date2DateTime = [DateTime]::ParseExact($Date2,'ddd,MMM d, yyyy, HH:mm:ss',[Globalization.CultureInfo]::InvariantCulture); $Date1DateTime -lt $Date2DateTime
You can only compare date strings if the string sort order is the same as the date sort order. For instance, date strings in ISO format are comparable:
2016-08-16T12:40:03
2016-08-22T16:33:02
Date strings in your custom format are not, because T comes after M, but August 16 should actually come before August 22:
Tue,Aug 16, 2016 12:40:03
Mon,Aug 22, 2016 16:33:02
If you don't have the date strings in ISO format it's usually better to parse them into actual DateTime values (as #PetSerAl suggested), particularly if your reference value is originally a DateTime anyway.
$fmt = 'ddd,MMM d, yyyy, HH:mm:ss'
$culture = [Globalization.CultureInfo]::InvariantCulture
$Date1 = Get-Date $LogFileDate
$val = (b2b.exe -readparams $param | Select-Object -Skip 1 -First 1) -split '='
$Date2 = [DateTime]::ParseExact($val[1], $fmt, $culture)
if ($Date1 -lt $Date2) {
...
}

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

Convert Long date to specific short date format

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