Timestamp

December 1, 2018 12:00 pm Published by

Un metodo per acquisire il timestamp in Java 8, utilizzando java.util.Date e String.format

private String getTime() {
  String result = "";
		
  //Get date/time
  Date date = new Date();

  //result = result + date.getYear() + "-";
  //result = result + date.getMonth() + "-";
  //result = result + date.getDay() + "-";
  //result = result + date.getHours() + "-";
  //result = result + date.getMinutes() + "-";
  //result = result + date.getSeconds() + "-";
  //result = result + date.getTime(); //Millisecondi
	    
  result = String.format("%1$tY %1$tm %1$td %1$tH %1$tM %1$tS %1$tL %1$tN ", date);
	    

/*
* https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
* 
The following date and time conversion suffix characters are defined forthe 't' and 'T' conversions. 
The types are similar to butnot completely identical to those defined by GNU date and POSIX strftime(3c). 
Additional conversion types are provided to accessJava-specific functionality 
(e.g. 'L' for milliseconds within thesecond). 
	    	 
'H'  Hour of the day for the 24-hour clock, formatted as two digits witha leading zero as necessary i.e. 00 - 23.  
'I'  Hour for the 12-hour clock, formatted as two digits with a leadingzero as necessary, i.e. 01 - 12.  
'k'  Hour of the day for the 24-hour clock, i.e. 0 - 23.  
'l'  Hour for the 12-hour clock, i.e. 1 - 12.  
'M'  Minute within the hour formatted as two digits with a leading zeroas necessary, i.e. 00 - 59.  
'S'  Seconds within the minute, formatted as two digits with a leadingzero as necessary, i.e. 00 - 60 ("60" is a specialvalue required to support leap seconds).  
'L'  Millisecond within the second formatted as three digits withleading zeros as necessary, i.e. 000 - 999.  
'N'  Nanosecond within the second, formatted as nine digits with leadingzeros as necessary, i.e. 000000000 - 999999999.  
'p'  Locale-specific morning or afternoon markerin lower case, e.g."am" or "pm". Use of the conversionprefix 'T' forces this output to upper case.  
'z'  RFC 822style numeric time zone offset from GMT, e.g. -0800. Thisvalue will be adjusted as necessary for Daylight Saving Time. For long, Long, and Date the time zone used isthe default time zone for thisinstance of the Java virtual machine.  
'Z'  A string representing the abbreviation for the time zone. Thisvalue will be adjusted as necessary for Daylight Saving Time. For long, Long, and Date the time zone used isthe default time zone for thisinstance of the Java virtual machine. The Formatter's locale willsupersede the locale of the argument (if any).  
's'  Seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000.  
'Q'  Milliseconds since the beginning of the epoch starting at 1 January1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.  

The following conversion characters are used for formatting dates: 

'B'  Locale-specific full month name, e.g. "January", "February".  
'b'  Locale-specific abbreviated month name,e.g. "Jan", "Feb".  
'h'  Same as 'b'.  
'A'  Locale-specific full name of the day of the week,e.g. "Sunday", "Monday"  
'a'  Locale-specific short name of the day of the week,e.g. "Sun", "Mon"  
'C'  Four-digit year divided by 100, formatted as two digitswith leading zero as necessary, i.e. 00 - 99  
'Y'  Year, formatted as at least four digits with leading zeros asnecessary, e.g. 0092 equals 92 CE for the Gregoriancalendar.  
'y'  Last two digits of the year, formatted with leading zeros asnecessary, i.e. 00 - 99.  
'j'  Day of year, formatted as three digits with leading zeros asnecessary, e.g. 001 - 366 for the Gregorian calendar.  
'm'  Month, formatted as two digits with leading zeros as necessary,i.e. 01 - 13.  
'd'  Day of month, formatted as two digits with leading zeros asnecessary, i.e. 01 - 31  
'e'  Day of month, formatted as two digits, i.e. 1 - 31.  

The following conversion characters are used for formatting commondate/time compositions. 

'R'  Time formatted for the 24-hour clock as "%tH:%tM"  
'T'  Time formatted for the 24-hour clock as "%tH:%tM:%tS".  
'r'  Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp".The location of the morning or afternoon marker ('%Tp') may belocale-dependent.  
'D'  Date formatted as "%tm/%td/%ty".  
'F'  ISO 8601complete date formatted as "%tY-%tm-%td".  
'c'  Date and time formatted as "%ta %tb %td %tT %tZ %tY",e.g. "Sun Jul 20 16:17:00 EDT 1969".  

Any characters not explicitly defined as date/time conversion suffixesare illegal and are reserved for future extensions. 
*/
	    

   return result;
}

Un altro esempio in VB6

Format(Now, "yyyyMMddhhmmss")

Categorised in: ,

This post was written by admin

Comments are closed here.