-----------------
java.util.Date today = new java.util.Date();
Timestamp currentTimestamp = new java.sql.Timestamp(today.getTime());
OR
Time time = Calendar.getInstance().getTime();
Current timestamp in String
---------------------------
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp now = new Timestamp(System.currentTimeMillis());
String str = df.format(now);
Get current year and month
---------------------------
SimpleDateFormat fyear = new SimpleDateFormat("yyyy");
SimpleDateFormat fmonth = new SimpleDateFormat("M");
Date today = Calendar.getInstance().getTime();
int currentYear = Integer.parseInt(fyear.format(today));
int currentMonth = Integer.parseInt(fmonth.format(today));
System.out.println("current year:"+ currentYear);
System.out.println("current month:"+ currentMonth);
OUTPUT:
current year:2012
current month:3
Datetime comparison
-------------------
Calendar rightNow = Calendar.getInstance();
rightNow.add(Calendar.MILLISECOND, -iViewerSetting.MAX_TRANSACTION_TIMEOUT);
Date expiryTime = rightNow.getTime();
if(txn.getStartTime().after(expiryTime)){
//If expired, reset status to Idle and wait for next round to retry.
iClient.updateTmaSubSyncStatus(tmaSub, SYNCHRONIZATION_STATUS.IDLE);
}
Convert String to Timestamp
---------------------------
String dateString = "2010-01-01 08:00:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = new Timestamp(dateFormat.parse(dateString).getTime());
OR,
//get current timestamp in string
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);
沒有留言:
張貼留言