java-time

DateTimeFormatter trouble with a pattern

江枫思渺然 提交于 2019-12-07 10:29:55
问题 I am writing a stock program that (so far) gets the data from "Markit on Demand" through a request such as this: http://dev.markitondemand.com/Api/v2/Quote/xml?symbol=aapl This returns the data in xml, with various measures of the stock (Symbol, name, last price, change, time stamp, etc). I am having trouble creating a DateTimeFormatter in Java 8 to make the timestamp. One example of a timestamp: Fri Jul 18 15:59:00 UTC-04:00 2014 So far the pattern I have is as follows: EEE MMM d HH:mm:ss

Java 8: How to create DateTimeFormatter with milli, micro or nano seconds?

和自甴很熟 提交于 2019-12-07 08:22:07
问题 I need to create the formatter for parsing timestamps with optional milli, micro or nano fractions of second. For example, for my needs I see the following opportunity: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .append(DateTimeFormatter.BASIC_ISO_DATE) .appendLiteral('-') .append(DateTimeFormatter.ISO_LOCAL_TIME) .appendOffset("+HH:mm", "Z") .toFormatter(); Or it is also possible to use appendFraction(field, minWidth, maxWidth, decimalPoint) . However in these cases will it

Calculate Modified Julian Day in JSR-310

你说的曾经没有我的故事 提交于 2019-12-07 04:46:55
问题 How can I calculate the Modified Julian day from a JSR-310 class such as LocalDate ? (in JDK 8) Specifically, this is the calculation of the continuous count of days known as "Modified Julian day", not the date in the Julian calendar system. For example: LocalDate date = LocalDate.now(); long modifiedJulianDay = ??? 回答1: Short answer: LocalDate date = LocalDate.now(); long modifiedJulianDay = date.getLong(JulianFields.MODIFIED_JULIAN_DAY); Explanation: The Wikipedia article gives the best

Parsing date without month using DateTimeFormatter

纵然是瞬间 提交于 2019-12-07 04:03:59
问题 I try to parse a date with this format: ddYYYY . For example, I have the string 141968 , and I want to know that day = 14 and year = 1968 . I suppose I have to use directly a TemporalAccessor gave by DateTimeFormatter.parse(String) , but I cannot find how to use this result. While debugging I see the result is a java.time.Parsed which is not public but contains informations I want in field fieldValues . How can I parse this particular format? Thank you. 回答1: One approach is to default the

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

Getting Duration using the new dateTime API

对着背影说爱祢 提交于 2019-12-06 21:26:06
问题 I want to get a java.time.Duration instance representing the duration of 3 years, and i found 2 ways to do it (see code below). public static void main(String[] args) { Duration d1 = Duration.of(3, ChronoUnit.YEARS); //Runtime Exception Duration d2 = ChronoUnit.YEARS.getDuration().multipliedBy(3); System.out.println("d2="+d2.toDays()); //OUTPUT: d2=1095 } The first way, d1 , throws the following Exception at runtime: Exception in thread "main" java.time.temporal

Wildcard in DateTimeFormatter

风格不统一 提交于 2019-12-06 19:39:04
问题 I need to parse a string into a LocalDate . The string looks like 31.* 03 2016 in regex terms (i.e. .* means that there may be 0 or more unknown characters after the day number). Example input/output: 31xy 03 2016 ==> 2016-03-31 I was hoping to find a wildcard syntax in the DateTimeFormatter documentation to allow a pattern such as: LocalDate.parse("31xy 03 2016", DateTimeFormatter.ofPattern("dd[.*] MM yyyy")); but I could not find anything. Is there a simple way to express optional unknown

Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects

China☆狼群 提交于 2019-12-06 17:12:44
问题 I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts. I have just moved to java 8 and want to know how to do it properly. My solution with Java 7 was like: DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN) DATE_FORMATTER.print(value.getTime()) It helped me not to create a lots of redundant objects. So now when I moved to java 8 I want rewrite it properly but: LocalDate.fromDateFields(value).toString()) creates each time new LocalDate

Checking if LocalDateTime falls within a time range

三世轮回 提交于 2019-12-06 16:36:44
I have a time A which should fall within 90 minutes range of timeB (before and after). Example: if timeB is 4:00 pm , time A should be between 2:30pm (-90) to 5:30pm (+90) Tried the following : if(timeA.isAfter(timeB.minusMinutes(90)) || timeA.isBefore(timeB.plusMinutes(90))) { return isInRange; } Can you help me whats wrong with the logic here? As @JB Nizet said in the comments , you're using the OR operator ( || ). So you're testing if A is after B - 90 OR A is before B + 90 . If only one of the conditions is satisfied, it returns true . To check if A is in the range, both conditions must be

Grails 3 and Java 8 time support

ε祈祈猫儿з 提交于 2019-12-06 13:49:28
In grails 3 application, I see that new java.time classes are persisted to database. Dynamic queries and create criteria works. However, I am wondering if there is some better way how to store these time data to database. When I look to database I see some binary data, it seems it just serialize the time objects. Is there any similar plugin as joda-time-plugin or is there any way how to configure database mapping for java.time classes? Edit : Grails 3.1.1 has hibernate 5 so this is not an issue anymore... Grails I finally found a solution. First of all hibernate 5 implements persistent for