Android Obtener la marca de tiempo actual?
Quiero obtener la marca de tiempo actual así : 1320917972
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts = tsTemp.toString();
9 answers
La solución es :
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-12 12:45:43
Del blog de desarrolladores:
System.currentTimeMillis()
es el reloj estándar de "pared" (hora y fecha) que expresa milisegundos desde la época. El reloj de pared puede ser configurado por el usuario o la red telefónica (ver setCurrentTimeMillis(long)), por lo que el tiempo puede saltar hacia atrás o hacia adelante de manera impredecible. Este reloj solo debe usarse cuando la correspondencia con fechas y horas del mundo real es importante, como en una aplicación de calendario o reloj despertador. Las mediciones de intervalo o tiempo transcurrido deben utilizar un otro reloj. Si está utilizando System.currentTimeMillis()
, considere escuchar el ACTION_TIME_TICK
, ACTION_TIME_CHANGED
y ACTION_TIMEZONE_CHANGED
La intención emite para averiguar cuándo cambia la hora.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-08 04:40:56
Puede usar la clase SimpleDateFormat :
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
String format = s.format(new Date());
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-26 09:47:45
1320917972 es la marca de tiempo Unix usando el número de segundos desde las 00: 00: 00 UTC del 1 de enero de 1970. Puede usar la clase TimeUnit
para la conversión de unidades - de System.currentTimeMillis()
a segundos.
String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-13 15:33:09
Utilice el siguiente método para obtener la marca de tiempo actual. Funciona bien para mí.
/**
*
* @return yyyy-MM-dd HH:mm:ss formate date as string
*/
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTime = dateFormat.format(new Date()); // Find todays date
return currentDateTime;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-20 08:36:34
Es un uso sencillo:
long millis = new Date().getTime();
Si lo desea en formato particular, entonces necesita Formateador como a continuación
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString = dateFormat.format(new Date());
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-25 10:52:47
Aquí hay una marca de tiempo legible por humanos que se puede usar en un nombre de archivo, solo en caso de que alguien necesite lo mismo que yo necesitaba:
package com.example.xyz;
import android.text.format.Time;
/**
* Clock utility.
*/
public class Clock {
/**
* Get current time in human-readable form.
* @return current time as a string.
*/
public static String getNow() {
Time now = new Time();
now.setToNow();
String sTime = now.format("%Y_%m_%d %T");
return sTime;
}
/**
* Get current time in human-readable form without spaces and special characters.
* The returned value may be used to compose a file name.
* @return current time as a string.
*/
public static String getTimeStamp() {
Time now = new Time();
now.setToNow();
String sTime = now.format("%Y_%m_%d_%H_%M_%S");
return sTime;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-03 07:34:24
Sugiero usar la respuesta de Hits, pero agregar un formato de configuración regional, así es como Android Desarrolladores recomienda :
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
return dateFormat.format(new Date()); // Find todays date
} catch (Exception e) {
e.printStackTrace();
return null;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-14 18:28:37
Solo use
long millis = new Date().getTime();
Para obtener la hora actual en milis largos
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-28 10:26:56