tag studio programacion móviles example desarrollo curso aplicaciones android date long-integer

android - studio - Convertir de formato largo a fecha



tag android studio (3)

Puede usar el método setTime en la instancia de Date o en la Fecha del contructor (larga);

setTime(long time) Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. Date(long date) Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT

A continuación, utilice el formater de fecha simple

consulte http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

Quiero convertir el valor largo en cadena o fecha en este formato dd / mm / YYYY.

Tengo este valor en formato largo: 1343805819061.

¿Es posible convertirlo al formato de fecha?


Puedes usar la siguiente línea de código para hacer esto. Aquí timeInMilliSecond es valor largo.

String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(TimeinMilliSeccond));

O también puede usar el código de abajo también.

String longV = "1343805819061"; long millisecond = Long.parseLong(longV); // or you already have long value of date, use this instead of milliseconds variable. String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();

Referencia: - DateFormat y SimpleDateFormat

PS Cambie el formato de fecha de acuerdo a su necesidad.


java.util.Date dateObj = new java.util.Date(timeStamp);

Aquí timeStamp es su entero largo, que en realidad es una marca de tiempo en milisegundos, obtiene el objeto de fecha java, ahora puede convertirlo en cadena de esta manera

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MMddyyyy"); StringBuilder nowYYYYMMDD = new StringBuilder( dateformatYYYYMMDD.format( dateObj ) ); StringBuilder nowMMDDYYYY = new StringBuilder( dateformatMMDDYYYY.format( dateObj ) );