@Test public void number_of_booking_during_time_period_returns_1_if_queried_with_end_date_in_the_middle_of_an_existing_booking() { LocalDate booking_from = LocalDate.of(2015, 7, 1); LocalDate booking_to = LocalDate.of(2015, 7, 18); LocalDate query_from = LocalDate.of(2015, 6, 12); LocalDate query_to = LocalDate.of(2015, 7, 12); Date from_date = Date.from(booking_from.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); Date to_date = Date.from(booking_to.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); Booking booking1 = Booking.builder() .apartment(apartment) .customer(customer) .startDate(from_date) .endDate(to_date) .build(); repository.save(booking1); Date query_from_date = Date.from(query_from.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); Date query_to_date = Date.from(query_to.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); assertThat( repository.findNumberOfBookingDuringTimePeriod(apartment, query_from_date, query_to_date), is(1)); }
@Override public void nullSafeSet( PreparedStatement preparedStatement, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { if (value == null) { StandardBasicTypes.DATE.nullSafeSet(preparedStatement, null, index, session); } else { LocalDate ld = ((LocalDate) value); Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); Date time = Date.from(instant); StandardBasicTypes.DATE.nullSafeSet(preparedStatement, time, index, session); } }
/** * Check time zones on the IANA time and zone database * * @param args */ public static void main(String[] args) { ZoneId casaZone = ZoneId.of("Africa/Casablanca"); ZoneId defaultZone = TimeZone.getDefault().toZoneId(); LocalDate rightNow = LocalDate.now(); ZonedDateTime zoneDateTime = rightNow.atStartOfDay(casaZone); ZoneId parisZone = ZoneId.of("Europe/Paris"); LocalDateTime rightNowInTime = LocalDateTime.now(); ZonedDateTime rightNowInTimeInParis = rightNowInTime.atZone(parisZone); Instant thisMoment = Instant.now(); ZonedDateTime thisMomentInCasa = thisMoment.atZone(casaZone); ZoneId tokyoZone = ZoneId.of("Asia/Tokyo"); LocalDate thisInstantInTokyo = rightNowInTime.toLocalDate(); LocalDateTime timeFromThisMomentInTokyo = LocalDateTime.ofInstant(thisMoment, tokyoZone); System.out.println( String.format("In Tokyo right now is %s", timeFromThisMomentInTokyo.toString())); }
@Override protected ExitStatus doRead( StepContribution contribution, ExecutionContext context, ExcelRow item) { ExitStatus result = ExitStatus.EXECUTING; LocalDate thresholdDate = getMaxMonthsThresholdDate(item); if (thresholdDate != null) { // We store the threshold date in the context context.put( "threshold.date", Date.from(thresholdDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())); // We keep trace that we've stored something contribution.incrementWriteCount(1); result = COMPLETED_WITH_MAX_MONTHS; } return result; }
private Date toDate(final LocalDate localDate) { final Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); return Date.from(instant); }
/** * Given an expire date for a link and the previous days for the alert to be sent, calculates the * date in which the alert will be sent. Since the user does not specify a time, it is set to * 00:00. */ protected Date processAlertDate(String expireDate, String days) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate expireLocal = LocalDate.parse(expireDate, formatter); LocalDate alertLocal = expireLocal.minusDays(Long.parseLong(days)); return Date.from(alertLocal.atStartOfDay(ZoneId.systemDefault()).toInstant()); }
public Date getDate(LocalDate localDate) { return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); }
@Override public Date convert(LocalDate source) { return source == null ? null : Date.from(source.atStartOfDay(ZoneId.systemDefault()).toInstant()); }
public static Date fromLocalDateIn(LocalDate date, ZoneOffset timeZone) { return Date.from(date.atStartOfDay().toInstant(timeZone)); }
public static Date fromLocalDateInUTC(LocalDate date) { return Date.from(date.atStartOfDay().toInstant(ZoneOffset.UTC)); }
@Override public Date convert(LocalDate value) { return Date.from(value.atStartOfDay(ZoneOffset.UTC).toInstant()); }
@Override public Date convertToDatabaseColumn(LocalDate attribute) { Instant instant = attribute.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); return Date.from(instant); }
/** 日付を元にTimePointを生成します。 */ public static TimePoint of(LocalDate day) { return of(day, day.atStartOfDay()); }
public static Instant getInstant(LocalDate localDate) { return localDate.atStartOfDay().atZone(DEFAULT_ZONE_ID).toInstant(); }
public static void main(String[] args) throws JsonParseException, MalformedURLException, IOException, ClassNotFoundException, SQLException, JSONException { Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection("jdbc:h2:~/stories2009-2014", "sa", ""); // add application code here Statement statement = conn.createStatement(); statement.execute("drop table story IF EXISTS;"); statement.execute( "create table story(" + "id int primary key," + " title varchar(255)," + " url varchar(255)," + " localDate date," + " primaryImage varchar(255)," + " primaryImageCaption TEXT," + " primaryImageRightsInformation TEXT," + " subjects TEXT," + " station varchar(255)," + " state varchar(255)," + " place varchar(255)," + " keywords TEXT," + " latitude varchar(255)," + " longitude varchar(255)," + " mediaRSS varchar(255))"); statement.close(); String fileJson = "Localphotostories2009-2014-JSON.json"; JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(new File(fileJson)); int idx = 1; while (parser.nextToken() != JsonToken.END_ARRAY) { while (parser.nextToken() != JsonToken.END_OBJECT) { if (parser.getCurrentToken() == JsonToken.START_OBJECT) continue; parser.nextToken(); String title = parser.getText(); parser.nextToken(); parser.nextToken(); String url = parser.getText(); parser.nextToken(); parser.nextToken(); String date = parser.getText(); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("d/M/yyyy"); LocalDate localDate = LocalDate.parse(date, pattern); parser.nextToken(); parser.nextToken(); String primaryImage = parser.getText(); parser.nextToken(); parser.nextToken(); String primaryImageCaption = parser.getText(); parser.nextToken(); parser.nextToken(); String primaryImageRightsInformation = parser.getText(); parser.nextToken(); parser.nextToken(); String subjects = parser.getText(); parser.nextToken(); parser.nextToken(); String station = parser.getText(); parser.nextToken(); parser.nextToken(); String state = parser.getText(); parser.nextToken(); parser.nextToken(); String place = parser.getText(); parser.nextToken(); parser.nextToken(); String keywords = parser.getText(); parser.nextToken(); parser.nextToken(); String latitude = parser.getText(); parser.nextToken(); parser.nextToken(); String longitude = parser.getText(); parser.nextToken(); parser.nextToken(); String mediaRSS = parser.getText(); if (latitude.isEmpty() && longitude.isEmpty()) continue; Statement cooridinate = conn.createStatement(); ResultSet resultSet = cooridinate.executeQuery( "SELECT id From story where latitude = '" + latitude + "' and longitude = '" + longitude + "'"); while (resultSet.next()) { int id = resultSet.getInt(1); Double tempLatitude = Double.valueOf(latitude); tempLatitude += Math.random() / 100; Double tempLongitude = Double.valueOf(longitude); tempLongitude += Math.random() / 100; Statement updatestat = conn.createStatement(); updatestat.executeUpdate( "UPDATE story set latitude = '" + tempLatitude + "', longitude = '" + tempLongitude + "' where id = " + id); updatestat.close(); } cooridinate.close(); PreparedStatement stm = conn.prepareStatement( "insert into story(id," + " title," + " url," + " localDate," + " primaryImage," + " primaryImageCaption," + " primaryImageRightsInformation," + " subjects," + " station," + " state," + " place," + " keywords," + " latitude," + " longitude," + " mediaRSS) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); stm.setInt(1, idx++); stm.setString(2, title); stm.setString(3, url); java.util.Date from = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); stm.setDate(4, new Date(from.getTime())); stm.setString(5, primaryImage); stm.setString(6, primaryImageCaption); stm.setString(7, primaryImageRightsInformation); stm.setString(8, subjects); stm.setString(9, station); stm.setString(10, state); stm.setString(11, place); stm.setString(12, keywords); stm.setString(13, latitude); stm.setString(14, longitude); stm.setString(15, mediaRSS); stm.execute(); stm.close(); } } // Ballarat photographs data fileJson = "BallaratPhotographics.json"; factory = new JsonFactory(); parser = factory.createParser(new File(fileJson)); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JsonOrgModule()); JSONArray value = mapper.readValue(parser, JSONArray.class); for (int i = 0; i < value.length(); i++) { String title = value.getJSONObject(i).getString("FIELD1"); PreparedStatement stm = conn.prepareStatement( "insert into story(id," + " title," + " url," + " localDate," + " primaryImage," + " primaryImageCaption," + " primaryImageRightsInformation," + " subjects," + " station," + " state," + " place," + " keywords," + " latitude," + " longitude," + " mediaRSS) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); stm.setInt(1, idx++); stm.setString(2, value.getJSONObject(i).getString("FIELD1")); stm.setString(3, value.getJSONObject(i).getString("FIELD4")); java.util.Date from = Date.valueOf(value.getJSONObject(i).getString("FIELD5") + "-01-01"); stm.setDate(4, new Date(from.getTime())); stm.setString(5, value.getJSONObject(i).getString("FIELD3")); stm.setString(6, value.getJSONObject(i).getString("FIELD2")); stm.setString(7, ""); stm.setString(8, ""); stm.setString(9, ""); stm.setString(10, "VIC"); stm.setString(11, "Ballarat"); stm.setString(12, value.getJSONObject(i).getString("FIELD1")); stm.setString(13, value.getJSONObject(i).getString("FIELD6")); stm.setString(14, value.getJSONObject(i).getString("FIELD7")); stm.setString(15, "BPHOTO"); stm.execute(); stm.close(); } conn.close(); }
private String getDataString() { LocalDate date = jDate.getDate(); Instant instant = date.atStartOfDay().atZone(ZoneId.of("GMT")).toInstant(); // End of day - GMT return Formater.columnDate(Date.from(instant)); }