Koden nedan ger mig den aktuella tiden. Men den säger ingenting om millisekunder.
public static String getCurrentTimeStamp() {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}
Jag får datum i formatet 2009-09-22 16:47:08
(YYYYY-MM-DD HH:MI:Sec).
Men jag vill hämta aktuell tid i formatet 2009-09-22 16:47:08.128
((YYYYY-MM-DD HH:MI:Sec.Ms)- där 128 anger millisekunden.
SimpleTextFormat
fungerar bra. Här är den lägsta tidsenheten sekund, men hur får jag fram millisekund också?
Du behöver bara lägga till millisekundfältet i din datumformatsträng:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
I API doc of SimpleDateFormat beskrivs formatsträngen i detalj.
Prova detta:-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = new Date();
System.out.println(dateFormat.format(date));
eller
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));