時刻をAM-PM形式でフォーマットするJavaプログラム
この記事では、AM-PM形式で時刻をフォーマットする方法を理解します。書式設定文字列は、日付/時刻の値を文字列表現(フラットファイル、人間が読める形式の出力など)から読み書きする方法を記述します。
以下は同じのデモンストレーションです-
入力がであると仮定します −
Current date: Thu Mar 17 16:04:31 IST 2022
必要な出力は −
The current Time in AM/PM format is : 04.04 pm
アルゴリズム
Step 1 - START Step 2 - Declare a date object namely current_date that fetches the current date and time. Step 3 - Define the values. Step 4 - Declare an object ‘formatTime’ of class SimpleDateFormat. Step 5 - Use the function .format(current_date) to format the time to the specified format. Step 6 - Display the result Step 7 - Stop
例1
ここでは、ユーザーがプロンプトに基づいて入力を入力しています。
import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo {
public static void main(String[] args) {
System.out.println("The required packages have been imported");
Date current_date = new Date();
System.out.println("The current date is: " + current_date);
SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa");
String result_time = formatTime.format(current_date);
System.out.println("\nThe current Time in AM/PM format is : " + result_time);
}
} 出力
The required packages have been imported The current date is: Thu Mar 17 16:04:31 IST 2022 The current Time in AM/PM format is : 04.04 pm
例2
ここでは、標準偏差を計算する関数を定義しました。
import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo {
static void format_time(Date current_date){
SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa");
String result_time = formatTime.format( current_date);
System.out.println("\nThe current Time in AM/PM format is : " + result_time);
}
public static void main(String[] args) {
System.out.println("The required packages have been imported");
Date current_date = new Date();
System.out.println("The current date is: " + current_date);
format_time(current_date);
}
} 出力
The required packages have been imported The current date is: Thu Mar 17 16:04:31 IST 2022 The current Time in AM/PM format is : 04.04 pm
-
Androidで日付と時刻をフォーマットする方法は?
この例は、Androidで日付と時刻をフォーマットする方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://sche
-
Javaは現在の日時を取得します
Javaで現在の日付と時刻を取得するにはどうすればよいですか?このチュートリアルでは、Java8の3つの異なるメソッドを見ていきます。 日付と時刻の表示に使用されるクラスはLocalDateです。 、LocalTime 、LocalDateTime 。 現在の日付を取得 LocalDate クラスは日付を表すために使用されます。 GetCurrentDate.java import java.time.LocalDate; public class GetCurrentDate { public static void main(String[] args) {