ejemplos

uso de format(formato, valores ...)

formato, valores ...

String resultado

“%d”, 5

“5”

“%3d”, 5

‪‪  5”

“%.2f”, Math.PI

3,14

“%8.4f”, Math.PI

  3,1416”

“%8.2feuros”, 5.95

    5,95euros”

“%,10.0f euros”, 48000.95

    48.001 euros”

“%s %s”, “hola”, “muchacho”

“hola muchacho”

“hola %s”, “muchacho”

“hola muchacho”

“%5s”, “hola”

hola”

Date date= new Date();

"Fecha y hora: %tc", date

Fecha y hora: mar sep 13 09:18:18 CEST 2005

long date=

     System.currentTimeMillis();

"Fecha y hora: %tc", date

Fecha y hora: mar sep 13 09:18:18 CEST 2005

Date date= new Date();

"Son las %tH:%<tM", date

Son las 09:18

Date date= new Date();

"Hoy es %tA, %<te de %<tB de %<tY", date

        Hoy es martes, 13 de septiembre de 2005

 

Temas relacionados

15. Formatter (clase) java.util.Formatter