下面列出了java.sql.Timestamp#getTime ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void xtestUpdateObjectWithSqlDateAsString()
throws Exception
{
Timestamp ts = Timestamp.valueOf("2004-01-12 00:00:00.0");
java.sql.Date oldDate = new java.sql.Date(ts.getTime());
java.sql.Date newDate = new java.sql.Date(System.currentTimeMillis());
Operation op = StringDatedOrderFinder.orderId().eq(1);
op = op.and(StringDatedOrderFinder.processingDate().eq(ts));
StringDatedOrder order = StringDatedOrderFinder.findOne(op);
assertNotNull(order);
assertEquals(oldDate, order.getOrderDate());
order.setOrderDate(newDate);
StringDatedOrder order2 = StringDatedOrderFinder.findOne(op);
assertEquals(newDate, order2.getOrderDate());
}
/**
* Checks if the rememberMe token is expired
*
* @param storedDo
* @return
*/
private boolean isExpired(OpenIDRememberMeDO storedDo) {
Timestamp timestamp = storedDo.getTimestamp();
String expiry = IdentityUtil.getProperty(IdentityConstants.ServerConfig.OPENID_REMEMBER_ME_EXPIRY);
if (timestamp != null && expiry != null) {
long t0 = timestamp.getTime();
long t1 = new Date().getTime();
long delta = Long.parseLong(expiry) * 1000 * 60;
if (t1 - t0 > delta) {
log.debug("Remember Me token expired for user " + storedDo.getUserName());
return true;
}
}
return false;
}
/**
* Method to parse Druid timestamp.
*
* @param timestamp input format 'yyyy-MM-ddTHH:mm:ss.SSSZ'
* @return timestamp in seconds
*/
private Long parseTimeStamp(String timestamp) throws SherlockException {
if (timestamp != null) {
timestamp = timestamp.replace("T", " ").replace("Z", "");
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsedDate;
try {
parsedDate = df.parse(timestamp);
} catch (ParseException e) {
log.error("Druid timestamp parsing error!", e);
throw new SherlockException(e.getMessage(), e);
}
Timestamp tp = new java.sql.Timestamp(parsedDate.getTime());
return (tp.getTime() / 1000);
} else {
log.info("Found null timestamp in Druid response");
throw new SherlockException("Null Timestamp in Druid response");
}
}
public static long formatLong(Timestamp timestamp) throws StandardException {
int nanos = timestamp.getNanos();
// 1. Round milliseconds down to the nearest second, e.g. -1001 ms becomes -2000 ms.
// 2. Shift 3 decimal places to the left, so there's a total of 6 zeroes in the rightmost digits.
// 3. Divide nanoseconds by 1000 to produce microseconds, and add that to the final value.
long micros = (timestamp.getTime() - (nanos / 1000000)) * 1000 + (nanos / 1000);
return micros;
}
public static AsOfAttribute generate(String attributeName,
String busClassNameWithDots, String busClassName, boolean isNullablePrimitive,
boolean hasBusDate, RelatedFinder relatedFinder, Map<String, Object> properties,
boolean isTransactional, boolean isOptimistic,TimestampAttribute fromAttribute, TimestampAttribute toAttribute, Timestamp infinityDate,
boolean futureExpiringRowsExist, boolean toIsInclusive, Timestamp defaultDate, boolean isProcessingDate, boolean isInfinityNull)
{
AsOfAttribute e = null;
try
{
e = (AsOfAttribute) extractorWriter.createClass(attributeName,
busClassName, isInfinityNull).newInstance();
}
catch (Exception excp)
{
throw new RuntimeException("could not create class for "+attributeName+" in "+busClassName, excp);
}
e.fromAttribute = fromAttribute;
e.toAttribute = toAttribute;
e.infinityDate = infinityDate;
e.infinityTime = infinityDate.getTime();
e.futureExpiringRowsExist = futureExpiringRowsExist;
e.toIsInclusive = toIsInclusive;
e.defaultDate = defaultDate;
e.attributeName = attributeName;
e.isProcessingDate = isProcessingDate;
e.setAll(attributeName, busClassNameWithDots, busClassName, isNullablePrimitive, relatedFinder, properties, isTransactional);
return e;
}
/** @return the timestamp in the given position of the given result set
* May return null if the result set contains null in the specified position.
*/
static public Date getResultSetTimestamp(ResultSet rs, int columnIndex) throws SQLException {
final Timestamp ts = rs.getTimestamp(columnIndex);
if (ts == null) {
return null;
}
return new Date(ts.getTime());
}
public void changeTime(Notification notification, Timestamp ntime) {
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String nowyear = year + "-01-01 00:00:00";
Timestamp yeardate = Timestamp.valueOf(nowyear);
String nowday = year + "-" + month + "-" + day + " 00:00:00";
Timestamp date = Timestamp.valueOf(nowday);
// 此处转换为毫秒数
long millionSeconds = ntime.getTime();// 毫秒
long nowSeconds = System.currentTimeMillis();
long chazhi = nowSeconds - millionSeconds;
if (chazhi < 60000) {
notification.setTime("现在");
} else if (chazhi < 3600000) {
long n = chazhi / 60000;
notification.setTime(n + "分钟");
} else if (ntime.after(date)) {
notification.setTime(sdf3.format(ntime));
} else if (ntime.after(yeardate)) {
notification.setTime(sdf.format(ntime));
} else {
notification.setTime(sdf2.format(ntime));
}
}
public ProductTestStatus(String id, String name, String deploymentPattern, String deploymentPatternId,
String status, Timestamp testExecutionTime) {
this.id = id;
this.name = name;
this.deploymentPattern = deploymentPattern;
this.deploymentPatternId = deploymentPatternId;
this.status = status;
this.testExecutionTime = new Timestamp(testExecutionTime.getTime());
}
/**
* Gets a primary partition value in "yyyy-MM-dd" format from the current date minus the specified number of days.
*
* @param offsetInDays the number of days to subtract from the current date to produce the primary partition value
*
* @return the primary partition value as a date in "yyyy-MM-dd" format
*/
private String getTestPrimaryPartitionValue(long offsetInDays)
{
// Get the current timestamp from the database.
Timestamp currentTimestamp = herdDao.getCurrentTimestamp();
// Apply the offset in days to current timestamp.
Timestamp updatedTimestamp = new Timestamp(currentTimestamp.getTime() - offsetInDays * 86400000L); // 24L * 60L * 60L * 1000L
// Return the primary partition value as a date in "yyyy-MM-dd" format.
return new SimpleDateFormat(AbstractHerdDao.DEFAULT_SINGLE_DAY_DATE_MASK, Locale.US).format(updatedTimestamp);
}
public static Timestamp truncateFractionalSeconds(Timestamp timestamp) {
Timestamp truncatedTimestamp = new Timestamp(timestamp.getTime());
truncatedTimestamp.setNanos(0);
return truncatedTimestamp;
}
@Test
public void test31() {
Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
Date d = new Date(ts1.getTime());
assertFalse(ts1.equals(d), "Error ts1 != d");
}
@Test
public void test29() {
Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
java.util.Date d = new java.util.Date(ts1.getTime());
assertFalse(ts1.equals(d), "Error ts1 == d");
}
public static Timestamp addSecondsToTimestamp(Timestamp start, int seconds) { // SCIPIO
return new Timestamp(start.getTime() + (1000L*seconds));
}
/**
* @param objectId
* @param client
*/
public static final void startEnterWorld(final int objectId, final AionConnection client) {
// check if char is banned
PlayerAccountData playerAccData = client.getAccount().getPlayerAccountData(objectId);
if (playerAccData == null) {
log.warn("playerAccData == null " + objectId);
if (client != null) {
client.closeNow();
}
return;
}
if (playerAccData.getPlayerCommonData() == null) {
log.warn("playerAccData.getPlayerCommonData() == null " + objectId);
if (client != null) {
client.closeNow();
}
return;
}
Timestamp lastOnline = playerAccData.getPlayerCommonData().getLastOnline();
Player edit = playerAccData.getPlayerCommonData().getPlayer();
if (lastOnline != null && client.getAccount().getAccessLevel() < AdminConfig.GM_LEVEL && edit != null && !edit.isInEditMode()) {
if (System.currentTimeMillis() - lastOnline.getTime() < (GSConfig.CHARACTER_REENTRY_TIME * 1000)) {
client.sendPacket(new SM_ENTER_WORLD_CHECK((byte) 6)); // 20 sec time
client.sendPacket(new SM_AFTER_TIME_CHECK());// TODO
return;
}
}
CharacterBanInfo cbi = client.getAccount().getPlayerAccountData(objectId).getCharBanInfo();
if (cbi != null) {
if (cbi.getEnd() > System.currentTimeMillis() / 1000) {
client.close(new SM_QUIT_RESPONSE(), false);
return;
}
else {
DAOManager.getDAO(PlayerPunishmentsDAO.class).unpunishPlayer(objectId, PunishmentType.CHARBAN);
}
}
// passkey check
if (SecurityConfig.PASSKEY_ENABLE && !client.getAccount().getCharacterPasskey().isPass()) {
showPasskey(objectId, client);
}
else {
validateAndEnterWorld(objectId, client);
}
}
@Test
public void test33() {
Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
Time t1 = new Time(ts1.getTime());
assertFalse(ts1.equals(t1), "Error ts1 == t1");
}
@Test
public void test31() {
Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
Date d = new Date(ts1.getTime());
assertFalse(ts1.equals(d), "Error ts1 != d");
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
//System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
/////////// Timestamp ////////////////////////////////
Timestamp ta = new Timestamp(millis);
ta.setNanos(nanos);
if (!isEqual(ta.toLocalDateTime(), ta)) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(ta.toLocalDateTime(), ta);
throw new RuntimeException("FAILED: j.s.ts -> ldt");
}
if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(ldt, Timestamp.valueOf(ldt));
throw new RuntimeException("FAILED: ldt -> j.s.ts");
}
Instant inst0 = ta.toInstant();
if (ta.getTime() != inst0.toEpochMilli() ||
ta.getNanos() != inst0.getNano() ||
!ta.equals(Timestamp.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
}
inst = Instant.ofEpochSecond(secs, nanos);
Timestamp ta0 = Timestamp.from(inst);
if (ta0.getTime() != inst.toEpochMilli() ||
ta0.getNanos() != inst.getNano() ||
!inst.equals(ta0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> timestamp -> instant");
}
////////// java.sql.Date /////////////////////////////
// j.s.d/t uses j.u.d.equals() !!!!!!!!
java.sql.Date jsd = new java.sql.Date(millis);
if (!isEqual(jsd.toLocalDate(), jsd)) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(jsd.toLocalDate(), jsd);
throw new RuntimeException("FAILED: j.s.d -> ld");
}
LocalDate ld = ldt.toLocalDate();
if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(ld, java.sql.Date.valueOf(ld));
throw new RuntimeException("FAILED: ld -> j.s.d");
}
////////// java.sql.Time /////////////////////////////
java.sql.Time jst = new java.sql.Time(millis);
if (!isEqual(jst.toLocalTime(), jst)) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(jst.toLocalTime(), jst);
throw new RuntimeException("FAILED: j.s.t -> lt");
}
// millis precision
LocalTime lt = ldt_ms.toLocalTime();
if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
print(lt, java.sql.Time.valueOf(lt));
throw new RuntimeException("FAILED: lt -> j.s.t");
}
}
System.out.println("Passed!");
}
@Test
public void test32() {
Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
java.util.Date d = new Date(ts1.getTime());
assertFalse(ts1.equals(d), "Error ts1 != d");
}
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) {
Timestamp value = (Timestamp) sqlArg;
return new java.util.Date(value.getTime());
}
@Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) {
Timestamp timeStamp = (Timestamp) javaObject;
return super.javaToSqlArg(fieldType, new Date(timeStamp.getTime()));
}