Search in sources :

Example 1 with Calendar

use of java.util.Calendar in project dropwizard by dropwizard.

the class JodaDateTimeArgument method apply.

@Override
public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException {
    if (value != null) {
        if (calendar.isPresent()) {
            // We need to make a clone, because Calendar is not thread-safe
            // and some JDBC drivers mutate it during time calculations
            final Calendar calendarClone = (Calendar) calendar.get().clone();
            statement.setTimestamp(position, new Timestamp(value.getMillis()), calendarClone);
        } else {
            statement.setTimestamp(position, new Timestamp(value.getMillis()));
        }
    } else {
        statement.setNull(position, Types.TIMESTAMP);
    }
}
Also used : Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Example 2 with Calendar

use of java.util.Calendar in project dropwizard by dropwizard.

the class ZonedDateTimeArgument method apply.

@Override
public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException {
    if (value != null) {
        if (calendar.isPresent()) {
            // We need to make a clone, because Calendar is not thread-safe
            // and some JDBC drivers mutate it during time calculations
            final Calendar calendarClone = (Calendar) calendar.get().clone();
            statement.setTimestamp(position, new Timestamp(value.toInstant().toEpochMilli()), calendarClone);
        } else {
            statement.setTimestamp(position, new Timestamp(value.toInstant().toEpochMilli()));
        }
    } else {
        statement.setNull(position, Types.TIMESTAMP);
    }
}
Also used : Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Example 3 with Calendar

use of java.util.Calendar in project dropwizard by dropwizard.

the class InstantArgumentTest method applyCalendar.

@Test
public void applyCalendar() throws Exception {
    final ZoneId systemDefault = ZoneId.systemDefault();
    // this test only asserts that a calendar was passed in. Not that the JDBC driver
    // will do the right thing and adjust the time.
    final ZonedDateTime zonedDateTime = ZonedDateTime.parse("2012-12-21T00:00:00.000Z");
    final ZonedDateTime expected = zonedDateTime.withZoneSameInstant(systemDefault);
    final Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone(systemDefault));
    new InstantArgument(zonedDateTime.toInstant(), Optional.of(calendar)).apply(1, statement, context);
    Mockito.verify(statement).setTimestamp(1, Timestamp.from(expected.toInstant()), calendar);
}
Also used : ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Test(org.junit.Test)

Example 4 with Calendar

use of java.util.Calendar in project Meizhi by drakeet.

the class Dates method isTheSameDay.

public static boolean isTheSameDay(Date one, Date another) {
    Calendar _one = Calendar.getInstance();
    _one.setTime(one);
    Calendar _another = Calendar.getInstance();
    _another.setTime(another);
    int oneDay = _one.get(Calendar.DAY_OF_YEAR);
    int anotherDay = _another.get(Calendar.DAY_OF_YEAR);
    return oneDay == anotherDay;
}
Also used : Calendar(java.util.Calendar)

Example 5 with Calendar

use of java.util.Calendar in project Meizhi by drakeet.

the class Dates method toDate.

public static String toDate(Date date, int add) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DATE, add);
    return toDate(calendar.getTime());
}
Also used : Calendar(java.util.Calendar)

Aggregations

Calendar (java.util.Calendar)9440 Date (java.util.Date)2432 GregorianCalendar (java.util.GregorianCalendar)2129 Test (org.junit.Test)1734 SimpleDateFormat (java.text.SimpleDateFormat)888 ArrayList (java.util.ArrayList)476 ParseException (java.text.ParseException)351 HashMap (java.util.HashMap)270 TimeZone (java.util.TimeZone)270 IOException (java.io.IOException)235 DateFormat (java.text.DateFormat)224 Timestamp (java.sql.Timestamp)192 List (java.util.List)187 File (java.io.File)167 Map (java.util.Map)149 BigDecimal (java.math.BigDecimal)134 Locale (java.util.Locale)134 Test (org.testng.annotations.Test)118 Identity (org.olat.core.id.Identity)112 Date (java.sql.Date)110