Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing and parsing of dates and times #444

Closed
yegor256 opened this issue Oct 18, 2017 · 7 comments
Closed

Printing and parsing of dates and times #444

yegor256 opened this issue Oct 18, 2017 · 7 comments

Comments

@yegor256
Copy link
Owner

yegor256 commented Oct 18, 2017

To be honest, I'm sick of date parsing and printing in Java 8 Date and Time API. How about we create our own object-oriented one at org.cactoos.time? Here is how it should work, I believe.

Printing:

String text = new DateAsText(new Date(), "%Y-%m-%d").asString();
String text = new DateAsText(System.currentTimeMillis(), "%Y-%m-%d").asString();
String text = new DateAsText(ZonedDateTime.now()).asString(); // ISO 8601 is the default one

Parsing:

Date date = new DateOf("2017-10-17", "%Y-%m-%d").value();
Date date = new DateOf("October 16, 2017", DateTimeFormatter.ISO_LOCAL_DATE).value();

With time zones:

String text = new DateAsText(new Date(), "%Y-%m-%d", "PST").asString();
Date date = new DateOf("2017-01-01", "%Y-%m-%d", ZoneOffset.UTC).value();

That's it.

Later, we can create, for example:

Date date = new Shifted(date, 5, "month").value();
@Timmeey
Copy link
Contributor

Timmeey commented Oct 18, 2017

Date date = new Shifted(date, 5, "month").value();

Please don't use "String" (month) as identifier for the unit. Noone can ever get the naming right. Was ist capitalized? Does ms exists as a uni? or was it even ms not MilliSecond?

Also i'm always struggling with date formatting since i can NEVER remember what character means what. Was MM minute or Month, or was it the month number or month name?

Maybe we could do something with enums here? At least the SHIFTING unit can definitely be done with an enum. Not so sure about the formatting yet.

@yegor256
Copy link
Owner Author

yegor256 commented Oct 19, 2017

@Timmeey we can use java.time.temporal.ChronoUnit enum: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html

@svendiedrichsen
Copy link
Contributor

svendiedrichsen commented Dec 10, 2017

@yegor256 I would like to contribute to this.

@yegor256
Copy link
Owner Author

@svendiedrichsen please, go ahead, but slowly. Don't make big pull requests, since they may get stuck in reviews.

@svendiedrichsen
Copy link
Contributor

@yegor256 Sorry for this. But I didn't think of 2 classes and 2 tests being such a big PR.

@svendiedrichsen
Copy link
Contributor

@yegor256 Can this be closed now?

@yegor256
Copy link
Owner Author

@svendiedrichsen yes, many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants