-
Notifications
You must be signed in to change notification settings - Fork 44
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
Java 8 LocalDate and DateTime compatibility #59
Comments
It would be awesome to add JodaTime/DateTime api support because now I have to mess between my code which uses new apis and old awkward java.time APIs |
Indeed, having to pass a |
Hi here, I do the transformation like this LocalDateTime start = event.getStartl();
Instant startInstant = start.atZone(zoneId).toInstant();
vEvent.setDateStart(Date.from(startInstant)); Please let me know if there's a better way! |
Ideally the new date / time classes would be supported directly ;-) |
Would it break Android compatibility if the new date/time classes were used? |
Android 8 features are supported without changing the minimum API level for android apps by using desugaring, see: So quick answer would be: It depends |
The fact that timezones are involved complicates this greatly. iCalendar files usually come with their own timezone definition data. In order to be fully compliant, biweekly would need to convert these VTIMEZONE components into custom A workaround would be to have the user supply their own TZID-to-ZoneId mapping so that biweekly can use File file = new File("event.ics");
ICalenar ical = Biweekly.parse(file)
.timezone("Eastern Standard Time", ZoneId.of("America/New_York"))
.go(); The code that generates dates from recurrence rules would also need to be overhauled. biweekly uses the old google-rfc-2445 project for this. |
After spending some time implementing this into a multi-threaded server. I removed it for this very reason as well as the need to save an .ics file to the hard drive. Any plans to replace the old Java Date class with Java.ZonedDateTime ? |
After a few hours of research in understanding how to convert this using Java 8+. The class below will convert a ZonedDateTime to the desired format for calendar coordination/invites. The users ZoneID can be obtained by using either: a ZonedDateTime can be created from several options. Conveniently if one has already created a LocalDateTime object then:
public static String from(ZonedDateTime zdt) {
} |
FYI, you don't need the second formatter: import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.of;
public class FormatterTest {
public static Stream<Arguments> zdtProvider() {
return Stream.of(
of(ZonedDateTime.of(LocalDateTime.of(1977, 9, 20, 16, 12), ZoneId.of("GMT+2"))),
of(ZonedDateTime.of(LocalDateTime.of(1979, 9, 9, 9, 2), ZoneId.of("GMT+12"))),
of(ZonedDateTime.of(LocalDateTime.of(2019, 6, 23, 6, 23), ZoneId.systemDefault())),
of(ZonedDateTime.now(ZoneOffset.systemDefault())),
of(ZonedDateTime.now(ZoneOffset.UTC))
);
}
@ParameterizedTest
@MethodSource("zdtProvider")
void verifyEquality(ZonedDateTime zdtOne) {
assertThat(from1(zdtOne)).isEqualTo(from2(zdtOne));
}
public static String from1(java.time.ZonedDateTime zdt) {
DateTimeFormatter fmtr1 = DateTimeFormatter.ofPattern("yyyyMMdd'T'hhmmss");
String str1 = zdt.format(fmtr1);
String zoneId = zdt.getZone().toString();
return zoneId + ":" + str1;
}
public static String from2(java.time.ZonedDateTime zdt) {
DateTimeFormatter fmtr1 = DateTimeFormatter.ofPattern("yyyyMMdd");
DateTimeFormatter fmtr2 = DateTimeFormatter.ofPattern("hhmmss");
String str1 = zdt.format(fmtr1);
String str2 = zdt.format(fmtr2);
String zoneId = zdt.getZone().toString();
return zoneId + ":" + str1 + "T" + str2;
}
} |
There could be added LocalDateIteratorFactory and DateTimeIteratorFactory as java.time support. In google-rfc-2445 there are already implemented similar iterators for jodatime compatibility.
The text was updated successfully, but these errors were encountered: