次のような現在のタイムスタンプを取得したい:1320917972
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts = tsTemp.toString();
解決策は:
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
開発者ブログから:
System.currentTimeMillis()
エポックからのミリ秒を表す標準の「壁掛け」時計(時刻と日付)です。壁掛け時計はユーザーまたは電話ネットワークで設定できるため(setCurrentTimeMillis(long)を参照)、時刻が予期せず前後にジャンプする可能性があります。この時計は、カレンダーや目覚まし時計のアプリケーションなど、実際の日付と時刻との対応が重要な場合にのみ使用してください。間隔または経過時間の測定では、別のクロックを使用する必要があります。を使用している場合は、、およびインテントブロードキャストをSystem.currentTimeMillis()
聞いて、時間がいつ変わるかを確認することを検討してください。ACTION_TIME_TICK
ACTION_TIME_CHANGED
ACTION_TIMEZONE_CHANGED
1320917972は、1970年1月1日の00:00:00UTCからの秒数を使用するUnixタイムスタンプです。TimeUnit
単位変換にクラスを使用できます-fromSystem.currentTimeMillis()
からseconds。
String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
SimpleDateFormatクラスを使用できます。
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
String format = s.format(new Date());
以下のメソッドを使用して、現在のタイムスタンプを取得します。それは私にとってはうまくいきます。
/**
*
* @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;
}
}
使い方は簡単です。
long millis = new Date().getTime();
特定のフォーマットが必要な場合は、以下のようなフォーマッターが必要です
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString = dateFormat.format(new Date());
誰かが私が必要としたものと同じものを必要とする場合に備えて、ファイル名に使用できる人間が読めるタイムスタンプを次に示します。
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;
}
}
以下のコードを試すことで、Androidの現在のタイムスタンプを取得できます
time.setText(String.valueOf(System.currentTimeMillis()));
およびtimeStampから時間形式
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(Long.parseLong(time.getText().toString())));
time.setText(dateString);
Kotlinのソリューション:
val nowInEpoch = Instant.now().epochSecond
SDKの最小バージョンが26であることを確認してください。
私は現代の答えに貢献したいと思います。
String ts = String.valueOf(Instant.now().getEpochSecond());
System.out.println(ts);
今実行中の出力:
1543320466
1000で割るのは多くの人にとって驚くことではありませんが、自分で時間変換を行うと、かなり速く読みにくくなる可能性があるため、回避できる場合に入るのは悪い習慣です。
Instant
私が使用しているクラスは、最新のJava date andtimeAPIであるjava.timeの一部です。これは、新しいAndroidバージョン、APIレベル26以降に組み込まれています。古いAndroid向けにプログラミングしている場合は、バックポートを取得できます。以下を参照してください。あなたがそれをしたくないのであれば、当然のことながら、私はまだ組み込みの変換を使用します:
String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
System.out.println(ts);
これはsealskejによる答えと同じです。出力は以前と同じです。
はい、java.timeは古いAndroidデバイスと新しいAndroidデバイスでうまく機能します。少なくともJava6が必要です。
org.threeten.bp
また、サブパッケージを使用して日付と時刻のクラスをインポートするようにしてください。java.time
。java.time
最初に説明されたJavaSpecificationRequest (JSR)310 。java.time
Java 6および7へのバックポート(JSR-310の場合はThreeTen)。Hitsの回答を使用することをお勧めしますが、ロケール形式を追加すると、Android開発者は次のように推奨します。
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;
}
このコードはKotlinバージョンです。分散エポック時間を与えるために、最後の桁にランダムなシャッフル整数を追加する別のアイデアがあります。
Kotlinバージョン
val randomVariance = (0..100).shuffled().first()
val currentEpoch = (System.currentTimeMilis()/1000) + randomVariance
val deltaEpoch = oldEpoch - currentEpoch
このコードを使用すると、Androidバージョン26以上に依存する方が良いと思います