How do I parse "YYYY-MM-DD" with joda time - date

I'm trying to use joda-time to parse a date string of the form YYYY-MM-DD. I have test code like this:
DateTimeFormatter dateDecoder = DateTimeFormat.forPattern("YYYY-MM-DD");
DateTime dateTime = dateDecoder.parseDateTime("2005-07-30");
System.out.println(dateTime);
Which outputs:
2005-01-30T00:00:00.000Z
As you can see, the DateTime object produced is 30 Jan 2005, instead of 30 July 2005.
Appreciate any help. I just assumed this would work because it's one of the date formats listed here.

The confusion is with what the ISO format actually is. YYYY-MM-DD is not the ISO format, the actual resulting date is.
So 2005-07-30 is in ISO-8601 format, and the spec uses YYYY-MM-DD to describe the format. There is no connection between the use of YYYY-MM-DD as a pattern in the spec and any piece of code. The only constraint the spec places is that the result consists of a 4 digit year folowed by a dash followed by a 2 digit month followed by a dash followed by a two digit day-of-month.
As such, the spec could have used $year4-$month2-$day2, which would equally well define the output format.
You will need to search and replace any input pattern to convert "Y" to "y" and "D" to "d".
I've also added some enhanced documentation of formatting.

You're answer is in the docs: http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
The string format should be something like: "yyyy-MM-dd".

The date format described in the w3 document and JodaTime's DateTimeFormat are different.
More specifically, in DateTimeFormat, the pattern DD is for Day in year, so the value for DD of 30 is the 30th day in the year, ie. January 30th. As the formatter is reading your date String, it sets the month to 07. When it reads the day of year, it will overwrite that with 01 for January.
You need to use the pattern strings expected by DateTimeFormat, not the ones expected by the w3 dat and time formats. In this case, that would be
DateTimeFormatter dateDecoder = DateTimeFormat.forPattern("yyyy-MM-dd");

Related

Convert a dates from Tweet hydrator to standard date format in Google Sheets mm/dd/yyyy

I have a large dataset (close to 80,000) of tweets dated like this:
Wed Oct 05 01:20:53 +0000 2016
What script can I run to convert the dates in Google Sheets to the simple mm/dd/yyyy form?
In this case, it should be: 10/05/2016
Thanks!
If the format of the date is you mentioned is consistent, you can use the below formula (assuming the date is in cell A1)
=DATEVALUE(RIGHT(A1,4) & MID(A1,5,3) & MID(A1,9,2))
This will extract the Datevalue from the string and then you can format it to look in the mm/dd/yyyy format
Try
=arrayformula(if(A1:A="",,1*(regexextract(A1,"\d{2}")&"/"&regexextract(A1,"\D+ (\D+) ")&"/"&regexextract(A1,".* (\d+)"))))
or (with hours/minutes/seconds)
=arrayformula(if(A1:A="",,1*(regexextract(A1,"\d{2}")&"/"&regexextract(A1,"\D+ (\D+) ")&"/"&regexextract(A1,".* (\d+)"))+regexextract(A1,"\d{2}:\d{2}:\d{2}")))
and define the appropriate format
Another solution
=index(ifna(Text(1&RegexExtract(A:A,".*?\s(.*?)\s"),"MM")&"/"&RegexExtract(A:A,"\d{2}")&"/"&RegexExtract(A:A,".*\s(.*)")))
Or
=index(text(regexreplace(regexreplace(A:A,"\+0000\s",),"(.*)(\d+:\d+:\d+)\s(.*)","$1$3 $2"),"mm/dd/yyyy"))
Or
=index(text(regexreplace(A:A,"(.*\s)(\d+:.*)\+.*\s(.*)","$1$3 $2"),"mm/dd/yyyy"))

DateTimeFormatter ofPattern not working for "L"

I have a LocalDateTime object and I would like to format this, to have printouts like:
Tue 23. Nov. Therefore, I used a DateTimeFormatter like:
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("e dd. LLL")
But unfortunately I get Tue 23. 11 The month is a number and no letters!?
The correct format pattern string is E dd. MMM. Excuse my Java syntax.
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("E dd. MMM", Locale.ENGLISH);
Also remember to specify desired locale for your formatter.
Trying it out:
LocalDate date = LocalDate.of(2021, Month.NOVEMBER, 23);
String formatted = date.format(DATE_FORMATTER);
System.out.println(formatted);
Output is the desired:
Tue 23. Nov
Spelling out how my format pattern is different:
I am using upper case E for the abbreviation of the day of week. Lower case e should give you the number of the day of week like 2 for Tuesday. eee should work for the abbreviation too.
I am using MMM for the abbreviation of the month. LLL is for the standalone form. Some languages use a different form of the month depending on whether the day of month is present or not. A language may for example use the nominative for the month alone and the genetive with a day number, a bit the differene between November and of November. Since you have the day included, you should not use pattern letter L here. Funnily for some languages that have not got a stand-alone form (like English), Java gives you the number instead when you specify LLL.
Edit: you asked:
How would that look for "November" fully written out? "MMM" works for
"Dec."
The documentation that you linked to in another comment gives the answer:
Text: The text style is determined based on the number of pattern letters used. Less than 4 pattern letters will use the short form.
Exactly 4 pattern letters will use the full form. …
So use MMMM instead of MMM:
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("E dd. MMMM", Locale.ENGLISH);
Tue 23. November
Documentation link: DateTimeFormatter

How to print current Time and Date in Q Basic?

What is the command for displaying the time and date in qbasic? Could the syntax for the commands be given as well? And an explanation if possible?
You can use DATE$ and TIME$
These can also set the date and time as well.
The command for printing the time(current system time) is time$
The time$ is actually a function, in this case, no parameter is needed.
And the code is...
PRINT TIME$
The time is printed in hh: mm: ss format(hour: minutes: seconds).
And therefore the output would be something like this:
14:55:28
For printing the current system date, we use date$ function which is also a string function
The code is:
PRINT DATE$
The date is printed in mm-dd-yyyy format or month-day-year(American date format).
Hence the output will be:
02-17-2018
Hope it helps...
The QB date/time functions are:
DATE$ returns the date in a string in the form MM-DD-YYYY
TIME$ returns the time in a string in the form HH:MM:SS
When used as a command the date$ and time$ can be assigned to set the system date and time, for example DATE$ = "12-10-1990" or TIME$ = "12:10:10"
If the year is a leap year then the 29th day of February could be set. Otherwise if it is not a leap year then a syntax error will occur trying to set the date in February to the 29th.

Transform string monthly dates in Stata

I have a problem in Stata with the format of the dates. I believe it is a very simple question but I can't see how to fix it.
I have a csv file (file.csv) that looks like
v1 v2
01/01/2000 1.1
01/02/2000 1.2
01/03/2000 1.3
...
01/12/2000 1.12
01/02/2001 1.1
...
01/12/2001 1.12
The form of v1 is dd/mm/yyyy.
I import the file in Stata using import delimited ...file.csv
v1 is a string variable, v2 is a float.
I want to transform v1 in a monthly date that Stata can read.
My attempts:
1)
gen Time = date(v1, "DMY")
format Time %tm
which gives me
Time
3177m7
3180m2
3182m7
...
that looks wrong.
2) In alternative
gen v1_1=v1
replace v1_1 = substr(v1_1,4,length(v1_1))
gen Time_1 = date(v1_1, "MY")
format Time_1 %tm
which gives exactly the same result.
And if I type
tsset Time, format(%tm)
it tells me that there are gaps but there are no gaps in the data.
Could you help me to understand what I'm doing wrong?
Stata has wonderful documentation on dates and times, which you should read from beginning to end if you plan on using time-related variables. Reading this documentation will not only solve your current problem, but will potentially prevent costly errors in the future. The section related to your question is titled "SIF-to-SIF conversion." SIF means "Stata internal form."
To explain your current issue:
Stata stores dates as numbers; you interpret them as "dates" when you assign a format. Consider the following:
set obs 1
gen dt = date("01/01/2003", "DMY")
list dt
// 15706
So that date is assigned the value 15706. Let's format it to look like a day:
format dt %td
list
// 01jan2003
Now let's format it to be a month:
format dt %tm
list
// 3268m11
Notice that dt is just a number that you can format and use like a day or month. To get a "month number" from a "day number", do the following:
gen mt = mofd(dt) // mofd = month of day
format mt %tm
list
// dt mt
// 3268m11 2003m1
The variable mt now equals 516. January 2003 is 516 months from January 1960. Stata's "epoch time" is January 1, 1960 00:00:00.000. Date variables are stored as days since the epoch time, and datetime variables are stored as miliseconds since the epoch time. A month variable can be stored as months since the epoch time (that's how the %tm formatting determines which month to show).

SAS date conversion from text.

Hi I have a date conversion problem in SAS,
I imported an excel file which has the following dates.,
2012-01-09
2011-01-31
2010-06-28
2005-06-10
2012-09-19
2012-09-19
2007-06-12
2012-09-20
2004-11-01
2007-03-27
2008-06-23
2006-04-20
2012-09-20
2010-07-14
after I imported the dates have changed like this
40917
40574
40357
38513
41171
41171
39245
41172
38292
39168
39622
38827
41172
40373
I have used the input function to convert the dates but it gives a strange result.,
the code I used.,
want_date=input(have_date, anydtdte12.);
informat want_date date9.; format have_date date9.;run;
I get very stange and out of the World dates., any idea how can I convert these?
You can encourage SAS to convert the data as date during the import, although this isn't necessarily a panacea.
proc import file=whatever out=whatever dbms=excel replace;
dbdsopts=(dbSasType=( datevar=date ) );
run;
where datevar is your date column name. This tells SAS to expect this to be a date and to try to convert it.
See So Your Data Are in Excel for more information, or the documentation.
From : http://www2.sas.com/proceedings/sugi29/068-29.pdf
Times are counted internally in SAS as seconds since midnight and
date/time combinations are calculated as the number of seconds since
midnight 1 January 1960.
Excel also uses simple numerical values for dates and times
internally. For the date values the difference with the SAS date is
only the anchor point. Excel uses 1 January 1900 as day one.
So add a constant.
EXAMPLES:
SAS_date = Excel_date - 21916;
SAS_time = Excel_time * 86400;
SAS_date_time = (Excel_date_time - 21916) * 86400;
As Justin wrote you need to correct for the different zero date (SAS vs. Excel).
Then you just need to apply a format (if you want to get a date variable to do calculations):
want_date = have_date-21916;
format want_date date9.;
Or convert it to a string:
want_date = put(have_date-21916, date9.);
In either case you can choose the date format you prefer.