Do you mean?
long millis = System.currentTimeMillis() % 1000;
BTW Windows doesn't allow timetravel to 1969
C:\> date
Enter the new date: (dd-mm-yy) 2/8/1969
The system cannot accept the date entered.
Answer from Peter Lawrey on Stack OverflowDo you mean?
long millis = System.currentTimeMillis() % 1000;
BTW Windows doesn't allow timetravel to 1969
C:\> date
Enter the new date: (dd-mm-yy) 2/8/1969
The system cannot accept the date entered.
Use Calendar
Calendar.getInstance().get(Calendar.MILLISECOND);
or
Calendar c=Calendar.getInstance();
c.setTime(new Date()); /* whatever*/
//c.setTimeZone(...); if necessary
c.get(Calendar.MILLISECOND);
In practise though I think it will nearly always equal System.currentTimeMillis()%1000; unless someone has leap-milliseconds or some calendar is defined with an epoch not on a second-boundary.
You can use Timestamp.getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.
Example:
long timeInMilliSeconds = t.getTime();
// do magic trick here
Note: Timestamp is extend from Date.
You can just call getTime() to get milliseconds since the Unix epoch. Is that what you were after, or did you want "milliseconds within the second" or something similar?
Note that using just milliseconds is slightly odd for a Timestamp, given that it's designed specifically to be precise to the nanosecond. So you should usually be using getTime() in conjunction with getNanos().
Use constructor.
new Timestamp(System.currentTimeMillis())
http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html#Timestamp(long)
This code snippet is used to convert timestamp in milliseconds to Unix based java.sql.Timestamp
/**
* Convert the epoch time to TimeStamp
*
* @param timestampInString timestamp as string
* @return date as timestamp
*/
public static Timestamp getTimestamp(String timestampInString) {
if (StringUtils.isNotBlank(timestampInString) && timestampInString != null) {
Date date = new Date(Long.parseLong(timestampInString));
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
Timestamp timeStamp = Timestamp.valueOf(formatted);
return timeStamp;
} else {
return null;
}
}