jodatime

How can I make Joda DateTime parser to only accept strings of form yyyyMMdd?

≡放荡痞女 提交于 2019-12-07 06:13:47
问题 I would like to use Joda-Time to parse dates in the format yyyyMMdd (so the date should have eight digits). I defined my date formatter as follows DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd").withZoneUTC(); // a valid eight-digit date String dateValid = "20130814"; DateTime dateJoda = formatter.parseDateTime(dateValid); System.out.println(dateJoda.toString()); // an invalid seven digit date String dateInvalid = "2013081"; dateJoda = formatter.parseDateTime(dateInvalid);

Adding a number of days to a JodaTime Instant

痴心易碎 提交于 2019-12-07 04:47:02
问题 I'm trying to write a simple utility method for adding aninteger number of days to a Joda time instant. Here is my first stab. /** * Adds a number of days specified to the instant in time specified. * * @param instant - the date to be added to * @param numberOfDaysToAdd - the number of days to be added to the instant specified * @return an instant that has been incremented by the number of days specified */ public static Instant addNumberOfDaysToInstant(final Instant instant, final int

PeriodFormatter not showing days

柔情痞子 提交于 2019-12-07 03:54:03
问题 I am using a PeriodFormatter to return a string showing the time until an event. The code below keeps creating strings like 1146 hours 39 minutes instead of x days y hours z minutes. Any ideas? Thanks, Nathan PeriodFormatter formatter = new PeriodFormatterBuilder() .printZeroNever() .appendDays() .appendSuffix( "d " ) .appendHours() .appendSuffix( "h " ) .appendMinutes() .appendSuffix( "m " ) .toFormatter(); return formatter.print( duration.toPeriod() ); 回答1: This is because you convert

Spring Boot ignores @JsonDeserialize and @JsonSerialize

妖精的绣舞 提交于 2019-12-07 02:53:11
问题 I've a Spring Boot application with RESTful endpoints that I want to add custom serializers to for joda-time but I can't get the applications default Jackson serailzier to recognize my custom one's. I created RESTFul endpoints using @RepositoryRestResource @RepositoryRestResource(collectionResourceRel = "x", path = "x") public interface XRepository extends PagingAndSortingRepository<X, Long> { } I then have a GET call to return all object X's: http://localhost:8181/x This is my serializer:

Validating Timestamp format yyyy-MM-dd'T'HH:mm:ssZ in java?

血红的双手。 提交于 2019-12-07 02:11:51
问题 I'm trying to do a timestamp validation using joda time-1.6.2 . Please point my error and help me out. Code String timestamp = "2014-09-23T23:03:11Z"; String datePattern = "yyyy-MM-dd'T'HH:mm:ssZ"; try { DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(datePattern); dateFormatter.parseDateTime(timestamp); } catch (Exception e) { LOG.info("Timestamp is invalid format" + e); } Exception INFO: Timestamp is invalid formatjava.lang.IllegalArgumentException: Invalid format: "2014-09

calculate weeks for one month in jodatime

坚强是说给别人听的谎言 提交于 2019-12-07 01:35:36
问题 Is it possible to calculate the weeks of one month in jodatime? I need something like this: Month: July Week of Year 27; 1-7. July Week of Year 28; 9-14. July Week of Year 29; 16-21. July Week of Year 30; 23-31. July Month: August Week of Year 31; 1-4. Aug Week of Year 32; 6-11. Aug Week of Year 33; 13-18. Aug Week of Year 34; 20-25. Aug Week of Year 35; 27-31. Aug I know that i can get the week of Year in joda time like this: new LocalDate().weekOfWeekYear() But i don´t know how to get the

Spring Boot ignore ObjectMapper module

余生颓废 提交于 2019-12-07 01:06:01
问题 In application context I have registered an ObjectMapper module: @Bean public SimpleModule jsr310Module() { final SimpleModule module = new SimpleModule(); module.addSerializer(new LocalDateSerializer()); module.addDeserializer(LocalDate.class, new LocalDateDeserializer()); return module; } I tried to debug and it is loaded (or at least the method public void setupModule(SetupContext context) is execute on boot) but when I call a rest API that return an object with a LocalDate my deserializer

Parse ISO date by Period in Java 8

丶灬走出姿态 提交于 2019-12-06 22:38:32
问题 i want to replace JodaTime by Java 8 DateTime API . I've got ISO-8601 period described = P2W5DT11H8M In JodaTime i parse it very simply by executing the following code: Period.parse("P2W5DT11H8M") and i get the successful Period object. Can i do the same in Java 8? 回答1: A Period in Java 8 only has year/month/day components. A Duration has hour/minute/second components. It seems that you will need to parse the string manually. One option could look like the code below (you need to add input

How to import FromString for joda-time?

谁都会走 提交于 2019-12-06 21:06:24
问题 I try to use joda-time library for easier measuring the execution time of my program (not for profiling, just for the user). But when I compile my project I get error about missing dependency -- the "FromString" class is missing. I tried to explicitly import it, but while typing intellisense (IntelliJ) does not even detect it, on the other hand there is only one jar for download from joda-time site. How do I resolve this dependency? I am aware of wrapper for JT but for now I would like to use

Android time in iso 8601

被刻印的时光 ゝ 提交于 2019-12-06 20:00:08
问题 using android and joda time lib - the I am trying to convert the user's timezone in order to format it later to : 2012-11-12T21:45:00+02:00 for example. DateTimeZone zone = DateTimeZone.forID( TimeZone.getDefault().getID()); the above code fails - anyone know how can I take "Europe/London" (Timezone.getID) and convert it to an offset so I can put it in ISO 8601 format? 回答1: If I have correctly understood your objective you can use directly the SimpleDateFormat class. Example code: