Javaで指定された日時の時間をミリ秒単位で取得するにはどうすればよいですか?
java.text.SimpleDateFormat classは、文字列を日付に、日付を文字列にフォーマットおよび解析するために使用されます。
- このクラスのコンストラクターの1つは、目的の日付形式を表すString値を受け入れ、SimpleDateFormatオブジェクトを作成します。
- 文字列をDateオブジェクトとして解析/変換するには目的のフォーマット文字列を渡して、このクラスをインスタンス化します。
- parse()メソッドを使用して日付文字列を解析します。
- getTime()メソッドを使用してエポックタイムを取得できます。
例
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Sample {
public static void main(String args[]) throws ParseException {
//Instantiating the SimpleDateFormat class
SimpleDateFormat dateformatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//Parsing the given String to Date object
String str = "25-08-2009 11:20:45";
Date date = dateformatter.parse(str);
long msec = date.getTime();
System.out.println("Epoch of the given date: "+msec);
}
} 出力
Epoch of the given date: 1251179445000
set()を使用して、カレンダーオブジェクトに日付と時刻の値を設定できます。 方法。このクラスのgetTimeInMillis()は、日付値のエポック時間を返します。
例
import java.util.Calendar;
public class Sample {
public static void main(String args[]) {
Calendar cal = Calendar.getInstance();
cal.set(2014, 9, 11, 10, 25, 30);
long msec = cal.getTimeInMillis();
System.out.print(msec);
}
} 出力
1413003330758
of()を使用して、日付と時刻の値をZonedDateTimeオブジェクトに設定できます。 方法。 InstantクラスのtoEpochMilli()は、日付値のエポック時間を返します。
例
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Sample {
public static void main(String args[]){
//Creating the ZonedDateTime object
ZoneId id = ZoneId.of("Asia/Kolkata");
ZonedDateTime obj = ZonedDateTime.of(2014, 9, 11, 10, 25, 30, 22, id);
Instant instant = obj.toInstant();
long msec = instant.toEpochMilli();
System.out.println("Milli Seconds: "+msec);
}
} 出力
Milli Seconds: 1410411330000
-
C ++で現在の時刻と日付を取得するにはどうすればよいですか?
これはC++言語で現在の日付と時刻を取得する例です 例 #include <iostream> using namespace std; int main() { time_t now = time(0); char *date = ctime(& now); cout << "The local date and time : " << date << endl; } 出力 これが出力です The local date and time
-
Windows11で日付と時刻を変更する方法
Windowsコンピュータの日付と時刻の設定は、通常、インターネットに接続するとすぐに自動的に設定されます。ただし、これらの設定を手動で変更することもできます。この記事では、Windows 11で日付と時刻を変更するために必要な正確な手順を説明しました。では、すぐに始めましょう。 Windows11で日付と時刻を変更する方法 Windows 11で日付と時刻を変更するには、Windowsの設定を利用する必要があります。開始するには、スタートメニューに移動します 検索バーに「設定」と入力して、最適なものを選択します。または、 Win + Iを使用することもできます ショートカット。 そこから