이 기사에서는 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