kotlin OffsetDateTime format of pattern - kotlin

I am try to convert a offsetdatetime to a specific format, but i have a problem
OffsetDateTime.parse("2021-12-27T15:49:08Z")
.format(DateTimeFormatter.ofPattern("EEE dd MMM YYYY, h:mm:ss"))
and it print one year more
lun. 27 dic. 2022, 3:49:08
what is the reason?
thanks a lot

This is because you're specifying the pattern YYYY for the year.
According to the docs for java.time.format.DateTimeFormatter, a pattern of one or more Ys means ‘week-based year’.
That's different from the usual ‘year-of-era’, which you get from a pattern of one or more lower-case ys.
The difference is explained in this question. Basically, if the year starts in the middle of a week, it treats that entire week as falling either in the previous year or the following year.
The exact calculation will depend indirectly upon the locale, as that controls which day the week starts on. (I don't fully understand all the details, but I think it's mediated by the WeekFields class. However it works, it can cause the last few days of 2021 to be treated as part of 2022, as this question demonstrates.)
In any case, it looks like your real problem is using a week-based year number when you just want the normal calendar year! So, simply change your pattern to "EEE dd MMM yyyy, h:mm:ss", and you should get the expected result:
lun. 27 dic. 2021, 3:49:08

Related

Print object properties using member function in Kotlin

I am really confused with the task I received from the Jetbrains Academy.
There is a calendar object which has day, month and year properties. Somebody has created a selectCurrentDay() member function that sets the object's properties to the current day. There is no need to type the date manually anymore because we have the selectCurrentDay() function!
Initially, the calendar shows a random date. Implement the current date printing: select it and print the day, the month and the year split by a space.
Output example:
21 12 2021
What i did was:
val calendar = createCalendar()
calendar.selectCurrentDay()
print("{$calendar.day} ${calendar.month} ${calendar.year}")
However my answer is rejected. What do they want me to implement?
print("${calendar.day} ${calendar.month} ${calendar.year}")

What is this date format called?

What is this date format called: 1YYMMDD
Example: 06/28/1959 outputs as 1590628
Example: 06/28/2019 outputs as 1190628
There is always a stinking 1 in front of it, no matter what.
As #TomNash already mentioned in his comment the 1 seems to be a century marker.
So your date format is CYYMMDD where C could be
0 : for 19xx
1 : for 20xx
This kind of format is not part of ISO 8601!
It looks to be a little bit like an IBM special and maybe AS/400 specific solution for the year-2000-problem.
My personal recommendation is to convert such date formats always to ISO 8601 via an intermediate layer before processing them.
Last but not least please remember to set software that use such outdated formats onto a list, because a solution like the century marker has only moved the year-2000-problem 100 years to the future - so the childs of our grandchilds will have the same problem again ...

ms-access built in function Month(number)

I Have been playing with variations of the Month... function in access query builder. I am having trouble building a date value from an expression. I am looking to create my own date that will be behind the scenes to perform some filtering and other tasks. My problem is that I cant seem to get the Month(number) function to do what I think it should be doing. Here is a summary of what I am looking for.
5/31/2012
Through something like this
DateSerial(Year(Date()),Month(5),Day(31))
Also
DateSerial(Year(Date()),Month("5"),Day("31"))
When I try these as an experssion the return is
1/30/2012
Im sure I am misunderstanding the structure. Please educate me.
DateSerial requires three integers, year, month, day:
DateSerial(1992,5,2)
02/05/1992 ''Euro locale
Year(Date()) returns an integer, so you can substitute:
DateSerial(Year(Date()),5,31)
Interestingly, the zeroth day is the last day of the previous month:
DateSerial(2012,12,0)=30/11/2012
-- http://office.microsoft.com/en-ie/access-help/HV080206953.aspx
As an aside, do not forget that all dates are numbers.
Month(5) will equal 1, but Month(41263)=12 !
Also
?month(100)
4
?Year(100)
1900

Date Formatting in iPhone SDk

I'm working on one iPhone application that includes date formatting. Here i'm getting date string from the server that i need to change and display of the formatted date.
Please find the below date m getting from the server
Server Date: Sat Apr 23 16:35:33 +0000 2011
This date i need to display like 2:35 AM Apr 23rd
Can anyone please help in this.
Thanks in advance.
You should use an NSDateFormatter to parse the date string you get from the server and get the corresponding NSDate (via -[NSDateFormatter dateFromString:]). You would then use a second NSDateFormatter to convert the NSDate to an NSString with the desired date format. Unless you absolutely require a very specific display format, you should use +[NSDateFormatter dateFormatFromTemplate:options:locale:] to get a date format that a) includes all the details you want it to include and b) respects the user’s locale settings. For instance, some users might prefer a 24-hour format instead of the 12-hour format with an AM/PM designation.

Returned Date as a string timestamp (Unix?) not sure

I'm working with some videogame server data. The server returns a dictionary with past game details. One of the fields is for the date. That returned object is a string like this:
/Date(1286749014000-0700)/
I'm not exactly sure how that string translates into the date, but it should represent Sunday, October 10, 2010, 3:16 PM.
Is this a Unix timestamp? Do they usually have a suffix like -0700?
Thank you
The number 1286749014 stands for 10 october 2010 5:16:54 pm. So if you substract the 700 from it you should get the right date and time.
Check out the Wikipedia article on Unix time for more information on how it's made up.
The first part ("128674901") exactly represents "Sun, 10 Oct 2010 22:16:54 GMT" date.
In objective-c you can use something like this:
NSTimeInterval unixDate = 128674901;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixDate];
It looks like you have a high resolution time with timezone offset.
The "0700" suffix is a time zone.
Which means UTC -07