jodatime

How to convert two digit year to full year using Java 8 time API

岁酱吖の 提交于 2019-12-04 03:01:21
问题 I wish to remove the Joda-Time library from my project. I am trying to convert a two digit year to full year. The following code from Joda-Time can fulfil the purpose. Below is the following code of joda-time DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy"); int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear(); System.out.println(year); Output: 1999 This is the output that I expect and that makes sense in my situation. However, when I try the same procedure

How do I parse a string like “-8y5d” to a Period object in joda time

你。 提交于 2019-12-04 02:39:57
I want to parse strings like "8y5d" or "-6y144d" into a joda time period object. Thanks Use the PeriodFormatterBuilder : PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder() .printZeroRarelyFirst() .appendYears() .appendSuffix("y", "y") .printZeroRarelyLast() .appendDays() .appendSuffix("d", "d") .toFormatter(); System.out.println(yearsAndMonths.parsePeriod("8y5d").toDurationFrom(new DateTime()).getStandardDays()); 来源: https://stackoverflow.com/questions/11669255/how-do-i-parse-a-string-like-8y5d-to-a-period-object-in-joda-time

Joda Time Interval between two dates including time zone

一曲冷凌霜 提交于 2019-12-04 02:10:07
I use JodaTime library for time operations. I have two dates: Date One: DateTime time_server = new DateTime(server_time_milisecs). withZone(DateTimeZone.forID("Europe/Zurich")); //+0100 Shows: 2013-01-27 13:44:42 Date two: DateTime time_local = new DateTime(DateTime.now()). withZone(DateTimeZone.getDefault()); //Have "Europe/Moscow" timezone +0400 Shows: 2013-01-27 16:41:47 I need to find real interval including timezone Interval interval = new Interval(time_local, time_server); Long.toString(interval.toDurationMillis())); Result: 174040 miliseconds - NOT CORRECT int secs = Seconds

Dozer mapping JodaTime property not working as expected

前提是你 提交于 2019-12-04 01:45:53
I am using Dozer to map between a Document class to DocumentManagementBean class, both of my own making. Both have a property, with getters and setters, of Joda DateTime type, called dateAdded. When Document object d has property dateAdded =x, calling mapper.map(d, DocumentManagementBean.class) all fields get auto-mapped correctly (since I have full control over code base I am able to get away with no dozer-config and rely simply on matching property names), EXCEPT the dateAdded field, where the new DocumentManagementBean dmb ends up with the current DateTime in its dateAdded property, instead

java.util.date to String using DateTimeFormatter

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 00:42:23
How can I convert a java.util.Date to String using DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss") The Date object which I get is passed DateTime now = new DateTime(date); assylias If you are using Java 8, you should not use java.util.Date in the first place (unless you receive the Date object from a library that you have no control over). In any case, you can convert a Date to a java.time.Instant using: Date date = ...; Instant instant = date.toInstant(); Since you are only interested in the date and time, without timezone information (I assume

java.time ISO date format with fixed millis digits (in Java 8 and later)

你。 提交于 2019-12-04 00:05:35
By default, the toString method of Instant uses the DateTimeFormatter.ISO_INSTANT formatter. That formatter won’t print the digits for fraction-of-second if they happen to be 0. java-time examples: 2015-10-08T17:13:07.589Z 2015-10-08T17:13:07Z Joda-Time examples (and what I'd expect from java.time): 2015-10-08T17:13:07.589Z 2015-10-08T17:13:07.000Z This is really frustrating to parse in some systems. Elasticsearch was the first problem I encountered, there's no pre-defined format that supports optional millis, but I can probably work around that with a custom format. The default just seems

Joda parse ISO8601 date in GMT timezone

烈酒焚心 提交于 2019-12-03 23:17:54
I have a ISO 8601 date, lets say: 2012-01-19T19:00-05:00 My machine timezone is GMT+1 I'm trying to use joda to parse this and convert it to the respective GMT date and time: DateTimeFormatter simpleDateISOFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mmZZ"); creationDate = simpleDateISOFormat.withZone(DateTimeZone.UTC) .parseDateTime(date + "T" + time) .toDate(); Now the result that I'm expecting is Fri Jan 20 00:00:00 CET 2012 Instead I'm getting: Fri Jan 20 01:00:00 CET 2012 I believe this is because I'm in timezone GMT + 1 . Is there a way to parse the date faking to be in a

Jodatime's LocalDateTime is slow when used the first time

こ雲淡風輕ζ 提交于 2019-12-03 22:51:41
I'm currently testing out some webapp technologies in a java project and was wondering why the pages sometimes load fast and sometimes take almost 5s to load. I finally found out that it is this line LocalDateTime now = new LocalDateTime(); When it's called the first time, it takes forever to get the current time. When called after that, even somewhere completely different, it's pretty fast however. I'm currently using <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>1.6.2</version> </dependency> Has anyone had any similar experience? Really stuck here.. I

How to deserialize Joda DateTime using Jackson with Jersey 2 Client in Spring MVC?

做~自己de王妃 提交于 2019-12-03 22:37:13
I've been bashing my head with this proof of concept for a while. I want to consume a REST endpoint that returns JSON payload with an ISO8601 UTC timestamp: { ... "timestamp" : "2014-08-20T11:51:31.233Z" } and I want to consume it using a command line Java client written as a Jersey 2 client with Jackson/Spring Boot. The marshalling POJO is defined as follows: @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(Include.NON_NULL) public class GreetingResource { @JsonProperty("timestamp") private DateTime date; ... } After following recommendations shown in: https://jersey.java.net

JodaTime DateTime, ISO8601 GMT date format

对着背影说爱祢 提交于 2019-12-03 22:14:31
How can I get the following format: 2015-01-31T00:00:00Z (ISO8601 GMT date format) Out of a DateTime object in joda time (java) ? Eg. DateTime time = DateTime.now(); String str = // Get something like 2012-02-07T00:00:00Z Thanks! :) The JODA Javadoc indicates that toString for DateTime outputs the date in ISO8601. If you need to have all of the time fields zeroed out, do this: final DateTime today = new DateTime().withTime(0, 0, 0, 0); System.out.println(today); That will include milliseconds in the output string. To get rid of them you would need to use the formatter that @jgm suggests here.