Android pobiera Aktualny znacznik czasu?

Chcę uzyskać bieżący znacznik czasu w ten sposób : 1320917972

int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts =  tsTemp.toString();
Author: Janusz, 2011-11-10

9 answers

Rozwiązaniem jest:

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
 227
Author: Rjaibi Mejdi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-12 12:45:43

Z bloga developerów:

System.currentTimeMillis() jest standardowym zegarem "ściennym" (czas i data) wyrażającym milisekundy od epoki. Zegar ścienny może być ustawiony przez użytkownika lub sieć telefoniczną( patrz setCurrentTimeMillis(long)), więc czas może przeskakiwać do tyłu lub do przodu nieprzewidywalnie. Zegar ten powinien być używany tylko wtedy, gdy ważna jest korespondencja z rzeczywistymi datami i czasami, na przykład w aplikacji kalendarza lub budzika. Pomiary interwału lub upływu czasu powinny inny zegar. Jeśli używasz System.currentTimeMillis(), rozważ słuchanie ACTION_TIME_TICK, ACTION_TIME_CHANGED / align = "left" /

 73
Author: drooooooid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-08 04:40:56

Możesz użyć klasy SimpleDateFormat :

SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
String format = s.format(new Date());
 22
Author: Pratik Butani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-26 09:47:45

1320917972 jest uniksowym znacznikiem czasu używającym liczby sekund od 00: 00: 00 UTC 1 stycznia 1970 roku. Możesz użyć klasy TimeUnit do konwersji jednostek-z System.currentTimeMillis() do sekund.

String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
 22
Author: sealskej,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-13 15:33:09

Użyj poniższej metody, aby uzyskać bieżący znacznik czasu. Mi pasuje.

/**
 * 
 * @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;
    }
}
 18
Author: Hits,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-20 08:36:34

To proste użycie:

long millis = new Date().getTime();

Jeśli chcesz go w konkretnym formacie, to potrzebujesz Formatera jak poniżej

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString  = dateFormat.format(new Date());
 9
Author: Pranav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-25 10:52:47

Oto czytelny dla człowieka znacznik czasu, który może być użyty w nazwie pliku, na wszelki wypadek ktoś potrzebuje tego samego co ja:

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;
    }

}
 8
Author: 18446744073709551615,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-03 07:34:24

Sugeruję użycie odpowiedzi Hits, ale dodanie formatu Locale, tak wygląda Android Deweloperzy poleca :

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;
    }
 0
Author: Faustino Gagneten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-14 18:28:37

Po prostu użyj

long millis = new Date().getTime();

Aby uzyskać bieżący czas w długich Milis

 0
Author: Bilal Ahmad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-28 10:26:56