Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/msangel/strftime4j
strftime time formatter for java
https://github.com/msangel/strftime4j
date-formatting java java-8 java-ports strftime time-formatting
Last synced: about 1 month ago
JSON representation
strftime time formatter for java
- Host: GitHub
- URL: https://github.com/msangel/strftime4j
- Owner: msangel
- License: mit
- Created: 2021-02-14T22:51:43.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T12:26:17.000Z (over 1 year ago)
- Last Synced: 2023-08-01T09:20:06.187Z (over 1 year ago)
- Topics: date-formatting, java, java-8, java-ports, strftime, time-formatting
- Language: Java
- Homepage:
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# strftime4j
strftime time formatter for javaSupport all the standard syntax of `strftime` function in C.
The Ruby's implementation was taken as an example spec(it also follows C conventions).
[![Build Status](https://travis-ci.com/msangel/strftime4j.svg?branch=master)](https://travis-ci.com/msangel/strftime4j)
[![Coverage Status](https://coveralls.io/repos/github/msangel/strftime4j/badge.svg?branch=master)](https://coveralls.io/github/msangel/strftime4j?branch=master) [![Maven Central](https://img.shields.io/maven-central/v/ua.co.k/strftime4j.svg?label=Maven%20Central)](https://central.sonatype.com/search?q=ua.co.k%3Astrftime4j)## Installing
Available in maven central:
```xmlua.co.k
strftime4j
1.0.6```
Or gradle:
`ua.co.k:strftime4j:+`## Usage example:
```java
// strict mode allow to ensure that the string will be formatted as expected:
ZonedDateTime t = ZonedDateTime.parse("2021-02-16T23:43:00-02:00");
String res = StrftimeFormatter.ofStrictPattern("Printed on %m/%d/%Y").format(t);
assertEquals("Printed on 02/16/2021", res);// strict mode will throw an exception if input object does
// not contains required temporal field
try {
YearMonth ym = YearMonth.of(2020, 1);
StrftimeFormatter.ofStrictPattern("Printed on %m/%d/%Y").format(ym);
} catch (Exception e) {
// exception caught,
// as the input object YearMonth does not
// contains information about the Date in it!
}// safe mode will ignore missing fields
YearMonth ym = YearMonth.of(2020, 1);
res = StrftimeFormatter.ofSafePattern("Printed on %m/%d/%Y").format(ym);
assertEquals("Printed on 01//2020", res);
```
Note:
Expression parsing behaves the same way in both strict and safe mode according to strftime function expectations. The difference between them is only in the way they handle missing date/time properties from the input date/time object during rendering time.## Supported types
* `java.util.Date` and its successors
* `java.util.Calendar`
* Java8 time API types, like:
* `java.time.Instant`
* `java.time.OffsetDateTime`
* `java.time.ZonedDateTime`
* `java.time.LocalDateTime`
* `java.time.ChronoLocalDateTime`
* `java.time.Year`
* any other `TemporalAccessor` successors## Thread-safety
This library is thread-safe.
After creation the `StrftimeFormatter` object is immutable and can be used in a multithreaded environment.## Possible future optimisations:
This library is ready for use.
But there are no limits for perfection.
Here's a list of some improvements, I'd like to implement here at some future:* Use `FromTemporalFieldFormat` instead of `PatternFormat` where possible, as in the end they both use the same low-level API, but in the first case - fewer objects will be created(also less memory and less stacktrace to the same important exception in the `strict` mode).
* Instead of `PatternFormat` create a new kind of `HybridFormat` that will work with conversion combinations and will delegate its parts rendering to known conversions.
* Expose method for short timezone names aliases mapping, so end-user be able to override short timezone names with own mapping.
* Keep codepoints list in `TextToken` as sublist from the input(it is immutable anyway). Less memory will be used.
## Publishing
On wiki page: https://github.com/msangel/strftime4j/wiki/Publish## License
Standard MIT license.In short:
* You may use the work commercially
* You may make changes to the work
* You may distribute the compiled code and/or source
* You may incorporate the work into something that has a more restrictive license.
* You may use the work for private use.
* The work is provided "as is". You may not hold the author liable.In long [read here](https://github.com/msangel/strftime4j/blob/master/LICENSE)
## Further reading:
* http://www.strfti.me/
* https://ruby-doc.org/core-3.0.0/Time.html#method-i-strftime
* https://www.php.net/manual/ru/function.strftime.php
* https://strftime.org/
* What version should be? https://octopus.com/blog/maven-versioning-explained