@Override Date transform(final Date value) { int year = maskedYearValue == UNMASKED_VAL ? value.getYear() : maskedYearValue; int month = maskedMonthValue == UNMASKED_VAL ? value.getMonth() : maskedMonthValue; int day = maskedDayValue == UNMASKED_VAL ? value.getDate() : maskedDayValue; return new Date(year, month, day); }
/** * Produces a string representation of the date in SQL format * * @return a string representation of the date in SQL format - {@code "yyyy-MM-dd"}. */ @Override public String toString() { StringBuilder sb = new StringBuilder(10); format((getYear() + 1900), 4, sb); sb.append('-'); format((getMonth() + 1), 2, sb); sb.append('-'); format(getDate(), 2, sb); return sb.toString(); }
/** * Test method for {@link com.impetus.kundera.property.accessor.SQLDateAccessor#fromString(java.lang.Class, java.lang.String)}. */ @Test public void testFromString() { Assert.assertNull(accessor.fromString(Date.class, null)); long l = System.currentTimeMillis(); Date d = new Date(l); Date d2 = accessor.fromString(Date.class, "" + l); Assert.assertEquals(d, d2); Assert.assertEquals(d.getYear(), accessor.fromString(Date.class, d2.toString()).getYear()); Assert.assertEquals(d.getMonth(), accessor.fromString(Date.class, d2.toString()).getMonth()); Assert.assertEquals(d.getDate(), accessor.fromString(Date.class, d2.toString()).getDate()); }
private void findByNameAndAgeWithOrClause() { EntityManager em; String query; Query q; List<StudentSqlDate> students; int count; em = emf.createEntityManager(); query = "Select s From StudentSqlDate s where s.name = 'Kuldeep' and s.age > " + getMinValue(short.class); q = em.createQuery(query); students = q.getResultList(); Assert.assertNotNull(students); Assert.assertEquals(1, students.size()); count = 0; for (StudentSqlDate student : students) { Assert.assertEquals(((Date) getMaxValue()).getDate(), student.getId().getDate()); Assert.assertEquals(((Date) getMaxValue()).getYear(), student.getId().getYear()); Assert.assertEquals(((Date) getMaxValue()).getMonth(), student.getId().getMonth()); Assert.assertEquals(getMaxValue(short.class), student.getAge()); Assert.assertEquals("Kuldeep", student.getName()); count++; } Assert.assertEquals(1, count); em.close(); }
private void findByAge() { EntityManager em; String query; Query q; List<StudentSqlDate> students; int count; em = emf.createEntityManager(); query = "Select s From StudentSqlDate s where s.age = " + getRandomValue(short.class); q = em.createQuery(query); students = q.getResultList(); Assert.assertNotNull(students); Assert.assertEquals(1, students.size()); count = 0; for (StudentSqlDate student : students) { Assert.assertEquals(((Date) getRandomValue()).getDate(), student.getId().getDate()); Assert.assertEquals(((Date) getRandomValue()).getYear(), student.getId().getYear()); Assert.assertEquals(((Date) getRandomValue()).getMonth(), student.getId().getMonth()); Assert.assertEquals(getRandomValue(short.class), student.getAge()); Assert.assertEquals(getRandomValue(String.class), student.getName()); count++; } Assert.assertEquals(1, count); em.close(); }
private void findByNameAndAgeGTAndLTEQ() { EntityManager em; String query; Query q; List<StudentSqlDate> students; int count; em = emf.createEntityManager(); query = "Select s From StudentSqlDate s where s.name = 'Kuldeep' and s.age > " + getMinValue(short.class) + " and s.age <= " + getMaxValue(short.class); q = em.createQuery(query); students = q.getResultList(); Assert.assertNotNull(students); Assert.assertEquals(1, students.size()); count = 0; for (StudentSqlDate student : students) { Assert.assertEquals(((Date) getMaxValue()).getDate(), student.getId().getDate()); Assert.assertEquals(((Date) getMaxValue()).getYear(), student.getId().getYear()); Assert.assertEquals(((Date) getMaxValue()).getMonth(), student.getId().getMonth()); Assert.assertEquals(getMaxValue(short.class), student.getAge()); Assert.assertEquals("Kuldeep", student.getName()); count++; } Assert.assertEquals(1, count); em.close(); }
private void findByNameAndAgeGTAndLT() { EntityManager em; String query; Query q; List<StudentSqlDate> students; int count; em = emf.createEntityManager(); query = "Select s From StudentSqlDate s where s.name = 'Amresh' and s.age > " + getMinValue(short.class) + " and s.age < " + getMaxValue(short.class); q = em.createQuery(query); students = q.getResultList(); Assert.assertNotNull(students); Assert.assertEquals(1, students.size()); count = 0; for (StudentSqlDate student : students) { Assert.assertEquals(((Date) getRandomValue()).getDate(), student.getId().getDate()); Assert.assertEquals(((Date) getRandomValue()).getYear(), student.getId().getYear()); Assert.assertEquals(((Date) getRandomValue()).getMonth(), student.getId().getMonth()); Assert.assertEquals(getRandomValue(short.class), student.getAge()); Assert.assertEquals(getRandomValue(String.class), student.getName()); count++; } Assert.assertEquals(1, count); em.close(); }
private void findByNameAndAGEBetween() { EntityManager em; String query; Query q; List<StudentSqlDate> students; int count; em = emf.createEntityManager(); query = "Select s From StudentSqlDate s where s.name = 'Amresh' and s.age between " + getMinValue(short.class) + " and " + getMaxValue(short.class); q = em.createQuery(query); students = q.getResultList(); Assert.assertNotNull(students); Assert.assertEquals(1, students.size()); count = 0; for (StudentSqlDate student : students) { Assert.assertEquals(((Date) getRandomValue()).getDate(), student.getId().getDate()); Assert.assertEquals(((Date) getRandomValue()).getYear(), student.getId().getYear()); Assert.assertEquals(((Date) getRandomValue()).getMonth(), student.getId().getMonth()); Assert.assertEquals(getRandomValue(short.class), student.getAge()); Assert.assertEquals(getRandomValue(String.class), student.getName()); count++; } Assert.assertEquals(1, count); em.close(); }
Assert.assertEquals(joiningDateAndTime.getSeconds(), s.getJoiningDateAndTime().getSeconds()); Assert.assertEquals(newSqlDate.getDate(), s.getSqlDate().getDate()); Assert.assertEquals(newSqlDate.getMonth(), s.getSqlDate().getMonth()); Assert.assertEquals(newSqlDate.getYear(), s.getSqlDate().getYear());
Assert.assertEquals(joiningDateAndTime.getSeconds(), s.getJoiningDateAndTime().getSeconds()); Assert.assertEquals(newSqlDate.getDate(), s.getSqlDate().getDate()); Assert.assertEquals(newSqlDate.getMonth(), s.getSqlDate().getMonth()); Assert.assertEquals(newSqlDate.getYear(), s.getSqlDate().getYear());
Assert.assertEquals(joiningDateAndTime.getSeconds(), s.getJoiningDateAndTime().getSeconds()); Assert.assertEquals(newSqlDate.getDate(), s.getSqlDate().getDate()); Assert.assertEquals(newSqlDate.getMonth(), s.getSqlDate().getMonth()); Assert.assertEquals(newSqlDate.getYear(), s.getSqlDate().getYear());
Assert.assertEquals(joiningDateAndTime.getSeconds(), s.getJoiningDateAndTime().getSeconds()); Assert.assertEquals(newSqlDate.getDate(), s.getSqlDate().getDate()); Assert.assertEquals(newSqlDate.getMonth(), s.getSqlDate().getMonth()); Assert.assertEquals(newSqlDate.getYear(), s.getSqlDate().getYear());
@SuppressWarnings("deprecation") private Date withTZ(Date date, Calendar cal) { if (date == null) return null; int year = date.getYear() + 1900; int month = date.getMonth(); int day = date.getDate(); return new Date(getMillis(cal, year, month, day, 0, 0, 0, 0)); }
/** * Converts a {@code java.sql.Date} to a {@code LocalDate}. * * @param sqlDate the SQL date, not null * @return the local date, not null */ @SuppressWarnings("deprecation") public static LocalDate toLocalDate(java.sql.Date sqlDate) { return LocalDate.of(sqlDate.getYear() + 1900, sqlDate.getMonth() + 1, sqlDate.getDate()); }
/** * Converts a {@code java.sql.Date} to a {@code LocalDate}. * * @param sqlDate the SQL date, not null * @return the local date, not null */ @SuppressWarnings("deprecation") public static LocalDate toLocalDate(java.sql.Date sqlDate) { return LocalDate.of(sqlDate.getYear() + 1900, sqlDate.getMonth() + 1, sqlDate.getDate()); }
@Test public void testFromCanonical() { Date coerced = (Date) sqlDateCoercibleType.coerce( getIntegerRepresentation().intValue(), java.sql.Date.class ); assertEquals( "wrong year", inputDate.getYear(), coerced.getYear() ); assertEquals( "wrong month", inputDate.getMonth(), coerced.getMonth() ); assertEquals( "wrong day of month", inputDate.getDate(), coerced.getDate() ); }