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

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);

Related

Flutter custom date format

I am facing issue while formatting the date to custom format.
I need to convert date yyyy-MM-dd HH:mm:ss ===> EEEE, MMM dd, yyyy
For example I am getting date from server 27-10-2022 11:02:50, and I need to convert it to Thursday, October 27, 2022
Getting Date format is "dd-MM-yyyy HH:mm:ss" and the desire format will be "EEEE, MMMM dd, yyyy"
final data = "27-10-2022 11:02:50";
final format = DateFormat("dd-MM-yyyy HH:mm:ss");
final DateTime result = format.parse(data);
print(result); //2022-10-27 11:02:50.000
final newFormatter = DateFormat("EEEE, MMMM dd, yyyy");
final newFormatString = newFormatter.format(result);
print(newFormatString); // Thursday, October 27, 2022
I am using intl package
Just checked in flutter docs. Your date format is not good. For converting string to date.
The following date format is required,
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456789z"
"2012-02-27 13:27:00,123456789z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
And you can convert that into the format like below
var date1 = DateFormat('dd-MM-yyyy hh:mm:ss').parse("27-10-2022 11:02:50");
var date2 = DateFormat('yyyy-MM-dd hh:mm:ss').format(date1);
print( DateFormat('EEEE, MMMM dd, yyyy').format(date2));
please try this, hope you will get the idea,
print(DateFormat('dd-MM-yyyy HH:mm:ss').parse('27-10-2022 11:02:50'));

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

pyspark : Convert string to date format without minute, decod and hour

Hello I would like to convert string date to date format:
for example from 190424 to 2019-01-24
I try with this code :
tx_wd_df = tx_wd_df.select(
'dateTransmission',
from_unixtime(unix_timestamp('dateTransmission', 'yymmdd')).alias('dateTransmissionDATE')
)
But I got this format : 2019-01-24 00:04:00
I would like only 2019-01-24
Any idea please?
Thanks
tx_wd_df.show(truncate=False)
You can simply use to_date(). This will discard the rest of the date, and pick up only the format that matches the input date format string.
import pyspark.sql.functions as F
date_column = "dateTransmission"
# MM because mm in Java Simple Date Format is minutes, and MM is months
date_format = "yyMMdd"
df = df.withColumn(date_column, F.to_date(F.col(date_column), date_format))

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.