¿Cuáles son los formatos de fecha disponibles en la clase SimpleDateFormat?

¿Puede alguien informarme sobre los formatos de fecha disponibles en la clase SimpleDateFormat?

I have gone through api but could not find a satisfactory answer.any help is highly appreciated.

Solución

Los formatos de fecha y hora se describen a continuación

SimpleDateFormat (Java Platform SE 7) - Patrones de fecha y hora

Podría haber n Número de formatos que posiblemente puede hacer. ex - dd/MM/yyyy o YYYY-'W'ww-u o puede mezclar y combinar las letras para lograr su patrón requerido. Las letras del patrón son las siguientes.

  • G - Designador de época (AD)
  • y - Año (1996; 96)
  • Y - Semana del año (2009; 09)
  • M` - Mes del año (julio; 07)
  • w - Semana del año (27)
  • W - Semana del mes (2)
  • D - Día del año (189)
  • d - Día del mes (10)
  • F - Día de la semana en el mes (2)
  • E - Nombre del día de la semana (martes; mar)
  • u - Número de día de la semana (1 = lunes, ..., 7 = domingo)
  • a - Marcador AM/PM
  • H - Hora del día (0-23)
  • k` - Hora del día (1-24)
  • K` - Hora en am/pm (0-11)
  • h - Hora en am/pm (1-12)
  • m - Minuto en hora (30)
  • s - Segundo en minuto (55)
  • S - Milisegundo (978)
  • z - Zona horaria general (Pacific Standard Time; PST; GMT-08:00)
  • Z - Zona horaria RFC 822 (-0800)
  • X - Zona horaria ISO 8601 (-08; -0800; -08:00)

Para analizar:

2000-01-23T04:56:07.000+0000

Utilizar: new SimpleDateFormat("aaaa-MM-dd'T'HH:mm:ss.SSSZ");

Comentarios (6)

Voy a poner un código de ejemplo que he sacado de http://www3.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html. Luego puedes jugar con diferentes opciones hasta que lo entiendas.

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {
   public static void main(String[] args) {
       Date now = new Date();

       //This is just Date's toString method and doesn't involve SimpleDateFormat
       System.out.println("toString(): " + now);  // dow mon dd hh:mm:ss zzz yyyy
       //Shows  "Mon Oct 08 08:17:06 EDT 2012"

       SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
       System.out.println("Format 1:   " + dateFormatter.format(now));
       // Shows  "Mon, 2012-10-8 at 8:17:6 AM EDT"

       dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
       System.out.println("Format 2:   " + dateFormatter.format(now));
       // Shows  "Mon 2012.10.08 at 08:17:06 AM EDT"

       dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
       System.out.println("Format 3:   " + dateFormatter.format(now));
       // Shows  "Monday, October 8, 2012"

       // SimpleDateFormat can be used to control the date/time display format:
       //   E (day of week): 3E or fewer (in text xxx), >3E (in full text)
       //   M (month): M (in number), MM (in number with leading zero)
       //              3M: (in text xxx), >3M: (in full text full)
       //   h (hour): h, hh (with leading zero)
       //   m (minute)
       //   s (second)
       //   a (AM/PM)
       //   H (hour in 0 to 23)
       //   z (time zone)
       //  (there may be more listed under the API - I didn't check)

   }

}

Buena suerte.

Comentarios (0)

consulte los formatos aquí http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

principal

System.out.println("date  : " + new classname().getMyDate("2014-01-09 14:06", "dd-MMM-yyyy E hh:mm a z", "yyyy-MM-dd HH:mm"));

Método

 public String getMyDate(String myDate, String returnFormat, String myFormat)
            {
                DateFormat dateFormat = new SimpleDateFormat(returnFormat);
                Date date=null;
                String returnValue="";
                try {
                    date = new SimpleDateFormat(myFormat, Locale.ENGLISH).parse(myDate);
                    returnValue = dateFormat.format(date);
                } catch (ParseException e) {
                    returnValue= myDate;
                    System.out.println("failed");
                    e.printStackTrace();
                }

            return returnValue;
            }
Comentarios (0)