jodatime

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

心不动则不痛 提交于 2019-12-05 07:49:35
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-23T23:03:11Z" is malformed at "Z" String timestamp = "2014-09-23T23:03:11Z"; DateTime dt = new DateTime

(Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

久未见 提交于 2019-12-05 07:28:05
I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result? Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to models where 24:00 could be represented as part of LocalTime , but the conclusion was that it would be very

How to compare two dates created as JodaTime LocalDate and LocalDateTime?

一个人想着一个人 提交于 2019-12-05 06:38:56
LocalDate startDate = new LocalDate(2014,1,2); LocalDateTime startDateTime = new LocalDateTime(2014,1,2,14,0); I need to compare startDate and startDateTime with respect to the date, something like this: // boolean equalDates = startDate.isequal(startDateTime.getDate()); Is it possible to do this? Aaron Digulla If you just want to compare the date part, you can do it like so: LocalDate startDate = new LocalDate(2014, 1, 2); LocalDateTime startDateTime = new LocalDateTime(2014, 1, 2, 14, 0); LocalDate forCompare = startDateTime.toLocalDate(); System.out.println("equal dates: " + forCompare

PeriodFormatter not showing days

☆樱花仙子☆ 提交于 2019-12-05 06:14:24
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() ); This is because you convert Duration to Period with method Duratoin::toPeriod This is described in Joda-time documentation: public Period

Adding a number of days to a JodaTime Instant

删除回忆录丶 提交于 2019-12-05 05:55:44
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 numberOfDaysToAdd) { Days days = Days.days(numberOfDaysToAdd); Interval interval = new Interval(instant,

Spring Boot ignores @JsonDeserialize and @JsonSerialize

陌路散爱 提交于 2019-12-05 05:47:33
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: @Component public class JsonDateSerializer extends JsonSerializer<DateTime> { private static

Can't get correct current islamic date

会有一股神秘感。 提交于 2019-12-05 04:21:00
i am using joda time to get current islamic date in Saudi Arabia as in their website: DateTime dtISO = new DateTime(2014, 1, 9, 0, 0, 0, 0); DateTime dtIslamic = dtISO.withChronology(IslamicChronology.getInstance()); above prints the day less than the actual day, it prints that today is 7 but actual is 8. please advise. UPDATE: i even tried it with timezone as follows: DateTimeZone SAUDI_ARABIA = DateTimeZone.forID("Asia/Riyadh"); DateTime dtISO = new DateTime(2014, 1, 9, 0, 0, 0, 0); DateTime dtIslamic = dtISO.withChronology(IslamicChronology.getInstance(SAUDI_ARABIA)); but it gives same

Problem adding minutes with plusMinutes

烈酒焚心 提交于 2019-12-05 04:14:16
I'm trying to add some minutes to a date using plusMinutes, but it just doesn't add anything at all: Here's the code: String currentDate ; SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); Date date1= null; DateTime dt; currentDate ="27/12/2010 11:29" ; try { date1= myFormat.parse(currentDate); } catch (ParseException ex) { ex.printStackTrace(); } dt = new DateTime(date1); dt.plusMinutes(30); Javadoc says Returns a copy of this datetime plus the specified number of millis. so do something like dt = new DateTime(date1); dt = dt.plusMinutes(30); System.out.println(""+dt);

Parse ISO date by Period in Java 8

雨燕双飞 提交于 2019-12-05 03:13:19
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? 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 validation etc.) - there may be better alternatives. public static void main(String[] args) { System.out

JodaTime with JPA, PostgreSQL and NULL values

五迷三道 提交于 2019-12-05 03:01:32
I'm trying to persists JodaTime DateTime fields with JPA to PostgreSQL but run into troubles with null pointers to database NULL values. I'm working with the NetBeans 7 beta 2 IDE. The persistence implementation is EclipseLink 2.2.0 and I'm using an EclipseLink Converter to get the mapping to work. Here is the declaration of my field: @Converter( name="dateTimeConverter", converterClass=ejb.util.DateTimeConverter.class ) @Column(columnDefinition="TIMESTAMP WITH TIME ZONE") @Convert("dateTimeConverter") private DateTime testdate; The converter class: public class DateTimeConverter implements