This question already has an answer here:
Date parsing in Go
(1 answer)
Closed 6 years ago.
I have this date, in this format:
25.04.2016
I need to parse this into a Golang time object so that I can store it in my DB.
What is the best way to do so? I can't find a standard parsing format that will do so.
To parse a date in go you provide a format string that represents the date Mon Jan 2 15:04:05 MST 2006 so in this case it would look like;
t, _ := time.Parse("02.01.2006", "25.04.2016")
(play ground example; https://play.golang.org/p/6E9zshNeFG )
Check the packages docs here; https://golang.org/pkg/time/#example_Parse
I believe the arbitrary date you use as the example format is the first day of the Go programming language.
Related
I want to get the 4-digit year from today.
I have a variable def todayDate = new Date() in my controller.
I googled to see how to do it. It pointed me to this page.
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html
There is a method called toYear()
Actually, none of the methods in this document works. The Date class in this document is not the same Date class in the controller for sure.
Did Google show me the wrong document? What is the correct way to get the 4-digit year from a Date()?
I was reading and trying to understand all the comments and checking the links. I think I get what is going on here.
When I declare a Date variable in Grails, it is a java.util.Date. If I google how to work with the Date, this gets confusing. The methods that process the Date are not from the java.util.Date class. Majority of the java.util.Date methods are deprecated. The methods you can find from google are actually coming from the org.codehaus.groovy:groovy-dateutil:3.0.9 Groovy date enhancement package.
So, the Date is a java.util.Date but the methods are from the enhancement package.
tl;dr
You said:
I want to get the 4-digit year from today.
String.valueOf( LocalDate.now().getYear() ) // Java syntax.
2022
Avoid legacy classes
The java.util.Date class is terribly flawed. Along with Calendar and the other legacy date-time classes, these were years ago supplanted by the modern java.time classes defined in JSR 310.
Sun, Oracle, and the JCP community all gave up on these legacy classes. I suggest you do the same.
Year
(Forgive the Java syntax as I do not know Groovy.)
If all you want is the year, use Year class.
Year currentYear = Year.now( z ) ;
int y = currentYear.getValue() ;
LocalDate#getYear
For a date-only value, use LocalDate.
Time zone
Determining the current date requires a time zone. For any given moment, the date varies around the globe by time zone.
(Again, Java syntax.)
ZoneId z = ZoneId.of( "America/Edmonton" ) ; // Or, ZoneId.systemDefault() ;
LocalDate today = LocalDate.now( z ) ;
int year = today.getYear() ;
I do not see all these methods in your Groovy API documentation. Perhaps I do not know how that doc works. The classes listed here are built into Java 8 and later.
I have the contract start of a number of companies, and I want to report on each contract year by creating a column with the contract start updated to a select year. There are a number of solutions in SQL involving functions like DATE_ADD or DATEFROMPARTS, but I'm having trouble adapting it to rails (if those functions are available at all).
The closest I've gotten is: Company.select("contract_start + '1 YEAR'::INTERVAL as new_contract_start"). This adds 1 year to each contract start but doesn't take into account contracts older than a year (or started the same year). I've also tried the following but again run into syntax errors:
new_year = 2020
Company.select("contract_start + '#{new_year} - EXTRACT (YEAR from contract_start) YEAR'::INTERVAL")
I'm looking for a solution that can either:
Directly set the year to what I want
Add a variable amount of years based on its distance from the desired year
I'm on Ruby 2.3.3
I think the key here was finding functions compatible with the PostgreSQL that my database was built on. Once I started searching for the functions I thought would help and their PostgreSQL equivalents, I found more compatible solutions, such as: NUMTODSINTERVAL in PostgreSQL
I ended up with:
contract_start_year = 2020
Company.select("contract_start + make_interval(years => CAST (#{contract_start_year} - EXTRACT (YEAR from contract_start) as INT))
I've also made it a bit smarter by adding the number of years required to get the latest contract date without going over the report date. This would be problematic if the report start date was "2020-01-01" but the contract start was "2017-06-01". Setting the contract date to "2020-06-01" would overshoot the intentions of the report.
report_start = "`2020-07-01`"
Company.select("contract_start + make_interval(years => CAST (EXTRACT (YEAR FROM AGE(CAST (#{start_quotations} AS DATE), contract_start)) AS INT)) as new_contract_year")
Note the additional single quotes in report_start since the SQL code need to read a string to convert it to a date
There might be other methods that can "build" the date directly, but this methods works well enough for now.
I want to compare string representations of weeks, e.g. week "01/17" is before "02/17" and after "52/16".
The following code throws an exception, I guess because my string doesn't hint at the exact day of each week. However, I don't care - it could all be Mondays or Thursdays or whatever:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ww/YY", Locale.GERMANY);
LocalDate date1 = formatter.parse(str1, LocalDate::from);
Do I need to modify the parser? Or parse to some other format? Unfortunatley there is no object like YearMonth for weeks...
One solution would be to always default to the same day, say the Monday. You could build a custom formatter for that:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("ww/YY")
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.toFormatter(Locale.GERMANY);
You can now build LocalDates representing the Monday of the given week:
LocalDate d1 = LocalDate.parse("01/17", fmt);
LocalDate d2 = LocalDate.parse("52/16", fmt);
System.out.println(d1.isAfter(d2));
which prints true because 01/17 is after 52/16.
I wasn't able to find a way for this to work with the DateTimeFormatter class, but I would like to suggest a different approach.
The Threeten Extra library contains a number of classes that were deemed too specific to include in the java.time library. One of them is the YearWeek class you mention.
Your problem can be solved by parsing the week-number and year manually from the input-string and then invoking the YearWeek creator-method like this:
YearWeek yw = YearWeek.of(year, monthOfYear);
tl;dr
YearWeek.parse( "2017-W01" )
ISO 8601
Or parse to some other format?
Yes, use another format.
Use the standard ISO 8601 formats when serializing date-time values to text. The standard includes support for week dates.
For a year-week that would be four year digits, a hyphen, a W, and two digits for the week of the year.
2017-W01
Get clear on your definition of a “week”. The ISO 8601 definition is that:
The Week # 1 contains the first Thursday of the year, and
Runs Monday-Sunday.
So years run either 52 or 53 weeks long. And note that under this definition, the first few days of the year may be in the prior year when week-numbering. Likewise, the last few days of the year may be in the following year when week-numbering.
If you want to indicate a particular day within that week, append a hyphen and a single digit running 1-7 for Monday-Sunday.
Tip: To see ISO 8601 week numbers by default on your computer, you may need to adjust your OS setting. For example, on macOS set System Preferences > Language & Region > Calendar > ISO 8601 to make apps such as Calendar.app to display week numbers with this standard definition.
2017-W01-7
By the way, a couple of similar representations:
An ordinal date meaning the year and the day-of-year-number running from 1-366 is year, a hyphen, and a three-digit number: 2017-123
Month-Day without year is two hyphens, month number, hyphen, and day-of-month number: --01-07
Note that the use of Locale as seen in the Question is irrelevant here with the standard ISO 8601 formats.
YearWeek
Unfortunatley there is no object like YearMonth for weeks...
Ahhh, but there is such a class.
For a class to directly represent the idea of a week-year, see the correct Answer by Henrik. That Answer shows the ThreeTen-Extra library’s class YearWeek.
The YearWeek class can directly parse and generate strings in standard format.
YearWeek yw = YearWeek.parse( "2017-W01" );
You can compare the YearWeek objects with methods: compareTo, equals, isBefore, isAfter.
yw.isBefore( thatYw )
The ThreeTen-Extra project offers other classes such as YearQuarter that you may find useful.
I developed a function in erlang which return date in this form for example: 15-Dec-2011
I want to convert this date in order to became in this case 15-12-2011 (so DEC became 12)
this is an example of date that should be converted
15-Dec-2011
19-Jan-2012
16-Feb-2012
15-Mar-2012
19-Apr-2012
17-May-2012
I will give basically the same answer with this post as the last post asking how to convert dates in erlang.
The easy solution is to use the ec_date or dh_date.
convert date in erlang
ec_date:format takes the format argument in a way similar to php's date function.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get a Date from a week number?
Finding the date for a given week number
How would I convert a week number (%W) back to a certain date. For example converting week 20 to May 15, 2012.
Check Date#commercial:
Date.commercial(2012, 20)
#=> Mon, 14 May 2012