下面列出了java.sql.Time#getTime ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Used to request the remaining capacity available for dateFrom in a TechDataCalenda,
* If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is zero.
*
* @param techDataCalendar The TechDataCalendar cover
* @param dateFrom the date
* @return long capacityRemaining
*/
public static long capacityRemainingBackward(GenericValue techDataCalendar, Timestamp dateFrom) {
GenericValue techDataCalendarWeek = null;
// TODO read TechDataCalendarExcWeek to manage exception week (maybe it's needed to refactor the entity definition
try {
techDataCalendarWeek = techDataCalendar.getRelatedOne("TechDataCalendarWeek", true);
} catch (GenericEntityException e) {
Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module);
return 0;
}
// TODO read TechDataCalendarExcDay to manage execption day
Calendar cDateTrav = Calendar.getInstance();
cDateTrav.setTime(dateFrom);
Map<String, Object> position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
int moveDay = (Integer) position.get("moveDay");
if (moveDay != 0) return 0;
Time startTime = (Time) position.get("startTime");
Double capacity = (Double) position.get("capacity");
Timestamp startAvailablePeriod = new Timestamp(UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + cDateTrav.get(Calendar.ZONE_OFFSET) + cDateTrav.get(Calendar.DST_OFFSET));
if (dateFrom.before(startAvailablePeriod)) return 0;
Timestamp endAvailablePeriod = new Timestamp(startAvailablePeriod.getTime()+capacity.longValue());
if (dateFrom.after(endAvailablePeriod)) return 0;
return dateFrom.getTime() - startAvailablePeriod.getTime();
}
private List<WebSocketChannelDTO> buildChannelDTOs(ResultSet rs) throws SQLException {
ArrayList<WebSocketChannelDTO> channels = new ArrayList<>();
try {
while (rs.next()) {
WebSocketChannelDTO channel = new WebSocketChannelDTO();
channel.id = rs.getInt("channel_id");
channel.host = rs.getString("host");
channel.port = rs.getInt("port");
channel.url = rs.getString("url");
channel.startTimestamp = rs.getTimestamp("start_timestamp").getTime();
Time endTs = rs.getTime("end_timestamp");
channel.endTimestamp = (endTs != null) ? endTs.getTime() : null;
channel.historyId = rs.getInt("history_id");
channels.add(channel);
}
} finally {
rs.close();
}
channels.trimToSize();
return channels;
}
private int convertFromTime(Schema schema, Time date) {
final LogicalType logicalType = schema.getLogicalType();
if (logicalType == LogicalTypes.timeMillis()) {
// adopted from Apache Calcite
final long time = date.getTime();
final long converted = time + (long) LOCAL_TZ.getOffset(time);
return (int) (converted % 86400000L);
} else {
throw new RuntimeException("Unsupported time type.");
}
}
@Test
public void test19() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertFalse(t.after(t2), "Error t.after(t2) = true");
assertFalse(t2.after(t), "Error t2.after(t) = true");
}
@Override
public Time copy(Time from) {
if (from == null) {
return null;
}
return new Time(from.getTime());
}
@Override
public LocalTime fromNonNullValue(Time value) {
DateTime dateTime = new DateTime(value.getTime());
LocalTime localTime = dateTime.toLocalTime();
return localTime;
}
/**
* opening_hours formats:
* Days: Mo | Tu | We | Th | Fr | Sa | Su
* Hours: 08:00-09:00
* Always Open: 24/7
* opening_hours example:
* 24/7
* 08:00-09:00
* Mo-Sa 10:00-20:00
* Fr 08:30-20:00
* Mo-Fr 10:00-20:00; Sa 10:00-14:00
* Tu-Th 18:00-03:00; Fr-Sa 18:00-04:00
* Only this formats are supported by this class,
* OSM has more opening_hours formats
* <a href="http://wiki.openstreetmap.org/wiki/Key:opening_hours">Key:opening_hours</a>.
*
* @param opening_hours OSM opening_hours String
* @return true if "NOW" is in the interval specified by opening_hours, false otherwise
*/
public static boolean isOpenNow(String opening_hours) {
try {
if (opening_hours == "" || opening_hours.equals("")) return false;
if (opening_hours.equals("24/7")) return true;
if (opening_hours.contains(";"))
return parseTwoTimeFrameOpeningHours(opening_hours);
if (opening_hours.replaceAll("\\s+", "").length() == "08:00-09:00".length()/* 11 */ && opening_hours.charAt(5) == '-') {
String startTime = opening_hours.substring(0, 5); //08:00
String endTime = opening_hours.substring(6); //09:00
Time openingHours = Time.valueOf(startTime.replaceAll("\\s+", "") + ":00");//add seconds, remove spaces
Time closingHours = Time.valueOf(endTime.replaceAll("\\s+", "") + ":00");//add seconds
Time now = Time.valueOf(now());
if (now.getTime() > openingHours.getTime() && now.getTime() < closingHours.getTime()) {
return true;
}
} else {
return parseDayFormatStrings(opening_hours);
}
return false;
} catch (Exception e) {
//there may be string construction exceptions
e.printStackTrace();
return false;
}
}
@Test
public void test19() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertFalse(t.after(t2), "Error t.after(t2) = true");
assertFalse(t2.after(t), "Error t2.after(t) = true");
}
@Test
public void test22() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertFalse(t.before(t2), "Error t.after(t2) = true");
assertFalse(t2.before(t), "Error t2.after(t) = true");
}
private static String getTimeAsString(Time time) {
if (null == time) {
return null;
}
Date date = new Date(time.getTime());
return getDateAsString(date);
}
@Test
public void test22() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertFalse(t.before(t2), "Error t.after(t2) = true");
assertFalse(t2.before(t), "Error t2.after(t) = true");
}
@Test
public void test22() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertFalse(t.before(t2), "Error t.after(t2) = true");
assertFalse(t2.before(t), "Error t2.after(t) = true");
}
@Test
public void test19() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertFalse(t.after(t2), "Error t.after(t2) = true");
assertFalse(t2.after(t), "Error t2.after(t) = true");
}
@Test
public void test26() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime() + 1);
assertTrue(t.compareTo(t2) == -1, "Error t.compareTo(t2) != -1");
}
@Test
public void test15() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertTrue(t.equals(t2) && t2.equals(t), "Error t != t2");
}
@Test
public void test25() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime() + 1);
assertTrue(t2.compareTo(t) == 1, "Error t2.compareTo(t) !=1");
}
@Test
public void testUsingDateTimeValuesWithFormatAttribute() throws InitializationException, ProcessException, SQLException, IOException, ParseException {
final TestRunner runner = TestRunners.newTestRunner(PutSQL.class);
try (final Connection conn = service.getConnection()) {
try (final Statement stmt = conn.createStatement()) {
stmt.executeUpdate("CREATE TABLE TIMESTAMPTEST3 (id integer primary key, ts1 TIME, ts2 DATE)");
}
}
runner.addControllerService("dbcp", service);
runner.enableControllerService(service);
runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp");
final String dateStr = "2002-03-04";
final String timeStr = "02:03:04";
final String timeFormatString = "HH:mm:ss";
final String dateFormatString ="yyyy-MM-dd";
final DateTimeFormatter timeFormatter= DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime parsedTime = LocalTime.parse(timeStr, timeFormatter);
Time expectedTime = Time.valueOf(parsedTime);
final DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
LocalDate parsedDate = LocalDate.parse(dateStr, dateFormatter);
Date expectedDate = new Date(Date.from(parsedDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()).getTime());
final long expectedTimeInLong = expectedTime.getTime();
final long expectedDateInLong = expectedDate.getTime();
// test with ISO LOCAL format attribute
Map<String, String> attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIME));
attributes.put("sql.args.1.value", timeStr);
attributes.put("sql.args.1.format", "ISO_LOCAL_TIME");
attributes.put("sql.args.2.type", String.valueOf(Types.DATE));
attributes.put("sql.args.2.value", dateStr);
attributes.put("sql.args.2.format", "ISO_LOCAL_DATE");
runner.enqueue("INSERT INTO TIMESTAMPTEST3 (ID, ts1, ts2) VALUES (1, ?, ?)".getBytes(), attributes);
// Since Derby database which is used for unit test does not have timezone in DATE and TIME type,
// and PutSQL converts date string into long representation using local timezone,
// we need to use local timezone.
SimpleDateFormat timeFormat = new SimpleDateFormat(timeFormatString);
java.util.Date parsedLocalTime = timeFormat.parse(timeStr);
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString);
java.util.Date parsedLocalDate = dateFormat.parse(dateStr);
// test Long pattern without format attribute
attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIME));
attributes.put("sql.args.1.value", Long.toString(parsedLocalTime.getTime()));
attributes.put("sql.args.2.type", String.valueOf(Types.DATE));
attributes.put("sql.args.2.value", Long.toString(parsedLocalDate.getTime()));
runner.enqueue("INSERT INTO TIMESTAMPTEST3 (ID, ts1, ts2) VALUES (2, ?, ?)".getBytes(), attributes);
// test with format attribute
attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIME));
attributes.put("sql.args.1.value", "020304000");
attributes.put("sql.args.1.format", "HHmmssSSS");
attributes.put("sql.args.2.type", String.valueOf(Types.DATE));
attributes.put("sql.args.2.value", "20020304");
attributes.put("sql.args.2.format", "yyyyMMdd");
runner.enqueue("INSERT INTO TIMESTAMPTEST3 (ID, ts1, ts2) VALUES (3, ?, ?)".getBytes(), attributes);
runner.run();
runner.assertAllFlowFilesTransferred(PutSQL.REL_SUCCESS, 3);
try (final Connection conn = service.getConnection()) {
try (final Statement stmt = conn.createStatement()) {
final ResultSet rs = stmt.executeQuery("SELECT * FROM TIMESTAMPTEST3 ORDER BY ID");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(expectedTimeInLong, rs.getTime(2).getTime());
assertEquals(expectedDateInLong, rs.getDate(3).getTime());
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals(parsedLocalTime.getTime(), rs.getTime(2).getTime());
assertEquals(parsedLocalDate.getTime(), rs.getDate(3).getTime());
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertEquals(expectedTimeInLong, rs.getTime(2).getTime());
assertEquals(expectedDateInLong, rs.getDate(3).getTime());
assertFalse(rs.next());
}
}
}
@Test
public void test26() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime() + 1);
assertTrue(t.compareTo(t2) == -1, "Error t.compareTo(t2) != -1");
}
@Override
public Timestamp getTimestamp(int columnIndex, TimeZone tz)
throws SFException
{
int columnType = resultSetMetaData.getColumnType(columnIndex);
if (Types.TIMESTAMP == columnType)
{
SFTimestamp sfTS = getSFTimestamp(columnIndex);
if (sfTS == null)
{
return null;
}
// If timestamp type is NTZ and JDBC_TREAT_TIMESTAMP_NTZ_AS_UTC=true, keep
// timezone in UTC to avoid daylight savings errors
if (resultSetSerializable.getTreatNTZAsUTC() &&
resultSetMetaData.getInternalColumnType(columnIndex) == Types.TIMESTAMP &&
columnType != SnowflakeUtil.EXTRA_TYPES_TIMESTAMP_LTZ &&
columnType != SnowflakeUtil.EXTRA_TYPES_TIMESTAMP_TZ)
{
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
Timestamp res = sfTS.getTimestamp();
if (res == null)
{
return null;
}
// If JDBC_TREAT_TIMESTAMP_NTZ_AS_UTC=false, default behavior is to honor
// client timezone for NTZ time. Move NTZ timestamp offset to correspond to
// client's timezone
if (!resultSetSerializable.getTreatNTZAsUTC() && honorClientTZForTimestampNTZ &&
resultSetMetaData.getInternalColumnType(columnIndex) == Types.TIMESTAMP)
{
res = sfTS.moveToTimeZone(tz).getTimestamp();
}
// Adjust time if date happens before year 1582 for difference between
// Julian and Gregorian calendars
Timestamp adjustedTimestamp = ResultUtil.adjustTimestamp(res);
return adjustedTimestamp;
}
else if (Types.DATE == columnType)
{
Date d = getDate(columnIndex, tz);
if (d == null)
{
return null;
}
return new Timestamp(d.getTime());
}
else if (Types.TIME == columnType)
{
Time t = getTime(columnIndex);
if (t == null)
{
return null;
}
return new Timestamp(t.getTime());
}
else
{
throw new SFException(ErrorCode.INVALID_VALUE_CONVERT, columnType, SnowflakeUtil.TIMESTAMP_STR,
getObjectInternal(columnIndex));
}
}
@Test
public void test24() {
Time t = Time.valueOf("08:30:59");
Time t2 = new Time(t.getTime());
assertTrue(t.compareTo(t2) == 0, "Error t.compareTo(t2) !=0");
}