SimpleDateFormat not reversible? - date

The following code is not behaving as expected when converting a data to a string, and back to a date under java 7 here:
final long epochTime = 1489669024142L;
String pattern = "ddMMMYY HH:mm";
final SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
final Date inputDate = new Date(epochTime);
final String date1 = format.format(inputDate);
final Date date2 = format.parse(date1);
The execution of this gives me:
inputDate: Thu Mar 16 13:57:04 CET 2017
date1: 16Mar17 13:57
date2: Sun Jan 01 13:57:00 CET 2017
Clearly, there is a good 3 months difference here, while I expected a 4 seconds difference.
I'm using java.util.Date, and cannot use JodaTime.
Any idea where this huge difference is coming from?

You have to use lower case y for year
Because upper case Y is Week year and lower case y is year. For more informations see the javadoc
String pattern = "ddMMMyy HH:mm";

Related

Trouble with converting to Datetime object

I have the below data which is an object type variable named $timestamps
Sat Jan 15 16:21:24
Sat Jan 15 01:31:22
Fri Jan 14 20:58:09
Fri Jan 14 20:51:02
I'm having trouble converting it to Datetime object because of the weird date format. How would you handle this?
I would like it as a datetime object because I plan to convert from current (UTC) to EST.
TIA
You can use the the ParseExact() method provided by the [datetime] class for this:
[datetime]::ParseExact('Fri Jan 14 20:58:09','ddd MMM dd HH:mm:ss',$null)
# returns a - datetime - object of:
# Friday, January 14, 2022 8:58:09 PM
dd - for the day.
MM - for the month.
HH - for the hour - Capitalized for the 24 hour time format.
mm - for the minutes.
ss - for the seconds.
Edit: as suggested by mklement0, we can use [cultureinfo]::InvariantCulture to make the parsing specific to an English date time format. Also, changing dd to d as a more robust solution for days without 2 digits; which should cover both singular, and double digit days.
Seeing $timestamps is an array of strings, you can use a loop (of your choice - in this case the Foreach-Object cmdlet) to iterate through each string parsing the text to return a datetime object:
$timestamps | ForEach-Object {
$culture = [cultureinfo]::InvariantCulture
$format = 'ddd MMM d HH:mm:ss'
$date = [datetime]::ParseExact($_,$format,$culture,'AssumeUniversal, AdjustToUniversal')
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date, 'Eastern Standard Time')
}
Using 'AssumeUniversal, AdjustToUniversal' ensures a UTC output.
Assuming from your comment that you'd like to do a conversion to Eastern Time, passing the newly created datetime object to [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId() with an argument of the desired time zone, you can get your result in the new time zone.
When using $null, the CultureInfo object that corresponds to the current culture is used.
The DateTime.ParseExact() method is probably what you're looking for.
PS C:\TEMP>$timestamp = 'Sat Jan 15 16:21:24'
PS C:\TEMP>$format = 'ddd MMM dd HH:mm:ss'
PS C:\TEMP>[datetime]::ParseExact($timestamp, $format, $null)
Saturday, January 15, 2022 04:21:24 PM
PS C:\TEMP>

in flutter dart convert 1624275605667 into Mon Jun 21 2021 17:10:05 GMT+05:30

var date = 1624275605667;
final DateTime formatted = DateTime(date);
final DateFormat fr = DateFormat('EEE MMM d yyyy HH:mm:ss');
final String dd = fr.format(formatted);
I try like this but getting some type of errors.
I want to convert 1624275605667 into this format Mon Jun 21 2021 17:10:05 GMT+05:30
For this which format I use here
DateFormat('EEE MMM d yyyy HH:mm:ss zzz')
Please try this one
var date = 1624275605667;
final DateTime formatted = DateTime.fromMillisecondsSinceEpoch(date);
final DateFormat fr = DateFormat('EEE MMM dd yyyy HH:mm:ss');
final String dd = fr.format(formatted);
print(dd);
You are using z pattern and it's not implemented yet. Issue is still open since 2015 https://github.com/dart-lang/intl/issues/19
And in intl package already mentioned that this characters are reserved and currently are unimplemented.
For workaround you can use
formatted.timeZoneOffset.toString(); /// 5:30:00.000000
Which is same as GMT+05:30

print first monday of every week of current month Flutter/Dart

I have a calendar in my Flutter App and I need to print a list of the weeks in the current month. But rather than starting on the 1st day of each month, it needs to start with the first Monday of the month (e.g. 05 April 2021 as the first Monday of April 2021). Then I need to print out the following weeks in that month, again starting from Monday. This includes the days of the next month that the final week of the current month follows on from (e.g. 26 April 2021 - 02 May 2021). It should print like this:
05 Apr - 11 Apr
12 Apr - 18 Apr
19 Apr - 25 Apr
26 Apr - 02 May
Start by figuring out how to find how many days there are from a given weekday to the Monday on or after that day. Examples help; if the given weekday is:
Monday, add 0 days.
Tuesday, add 6 days.
Wednesday, add 5 days.
... etc. ...
Sunday, add 1 day.
We could make a lookup table, but we also can devise that the offset (in days) from a given weekday to the Monday on or after that day has the form (7 - x) % 7
where x corresponds to the given weekday. We'd want that value to be 0 for Monday, 1 for Tuesday, and so on, until 6 for Sunday. Dart's DateTime.weekday uses values 1 (DateTime.monday) through 7 (DateTime.sunday), so we can easily map that to the value we want via DateTime.weekday - DateTime.monday.
Once we compute that offset, we can find the first day of the current month, add that offset to find the first Monday of the month, and then you can iteratively add 7 days until you reach the next month, and we can use DateFormat from package:intl to format the dates the way you want:
import 'package:intl/intl.dart';
String formatDate(DateTime dateTime) => DateFormat('dd MMM').format(dateTime);
void main() {
var now = DateTime.now();
var firstOfMonth = DateTime(now.year, now.month, 1);
var firstMonday =
firstOfMonth.addCalendarDays((7 - (firstOfMonth.weekday - DateTime.monday)) % 7);
var currentMonday = firstMonday;
while (currentMonday.month == now.month) {
var nextMonday = currentMonday.addCalendarDays(7);
var nextSunday = nextMonday.addCalendarDays(-1);
print('${formatDate(currentMonday)} - ${formatDate(nextSunday)}');
currentMonday = nextMonday;
}
}
See https://stackoverflow.com/a/68216029/ for the implementation of the addCalendarDays extension method.

how can i get the day of the week for a particular given date in Matlab

As today is Wednesday with date June 8, 2016. how can i write a code to get the day of given dates:
like what day is Nov 29
I'm trying to create a struct with
date
day
month
with month and date as input
Use the builtin weekday() function:
>> [num, name] = weekday('08-Jun-2016')
num =
4
name =
Wed
>> [num, name] = weekday('29-Nov-2016')
num =
3
name =
Tue
In addition to the weekday function, you can use the DDD or DDDD formats in the datestr function, like this:
datestr('08-Jun-2016','DDD') %Returns the string 'Wed'
datestr('08-Jun-2016','DDDD') %Returns the string 'Wednesday'
Or, to use a more practical format
datestr('08-Jun-2016','DDDD, mmmm DD, yyyy')
% Returns the string: 'Wednesday, June 08, 2016'

Converting string to date in ExtJs

I have a date in string format as follows-
"Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)"
I want to check whether the date is a month old or not.
For this I am doing like this-
var todaysDate= new Date();
todaysDate.setDate(todaysDate.getDate() - 30);
if(Ext.util.Format.dateRenderer("Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)", "D M d Y g:i:s") <= todaysDate)
{
}
It should return true but it is returning false. What I am doing wrong here.
Please help.
You're comparing a renderer to a date, dateRenderer doesn't return a date, it returns a renderer. Renderers are used for example for grid cells, when you have a grid that has a record with a date and you want to display that date in a particular format you use a renderer to tell the grid cell how to format that date.
Check the docs about this: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.util.Format-method-dateRenderer
Also I believe dateRenderer takes only one argument, you're passing two.
You can use regular Javascript for this:
var oldDate = new Date("Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)");
var newDate = new Date()
newDate.setDate(newDate.getDate() - 30);
if(oldDate <= newDate){
doSomething()
}