📜  以 AM-PM 格式格式化时间的Java程序

📅  最后修改于: 2022-05-13 01:54:49.806000             🧑  作者: Mango

以 AM-PM 格式格式化时间的Java程序

日期时间类格式用于在Java中显示日期和时间以及操作日期和时间,除此之外,它还用于跨时区相关数据格式化Java中的日期和时间类。所以为了从一个叫做' Java.utils'的包中导入这个类。导入此类后,可以创建 Date 类的对象以打印当前日期和时间。现在为了打印默认日期和时间,只需使用 toString() 方法调用打印命令来获取当前日期和时间。

如果通过代码库引用中任何时间的内置数据类函数调用计算的毫秒数设置为 1970 年 1 月 1 日。为了打印直到现在的毫秒数,只需使用 getTime() 而不是 to String() 来获取毫秒数直到现在。 假设用户是否需要当前时间特定日期、时间和月份。这可以通过DateSimpleDateFormat类来完成 Java。

方法:

  • 使用 SimpleDateFormat
  • 使用分割字符串

方法一:SimpleDateFormat

SimpleDateFormat类是Java中的一个类,它提供了多种方法来解析和格式化日期和时间。该类继承了Java.text.DateFormat类。 Java中 DateFormat 类的 format() 方法用于将给定日期格式化为 Date/Time 字符串。基本上,该方法用于将此日期和时间转换为特定格式,即“mm/dd/yyyy”。 format()方法用于更改以特定方式提供给它的参数的格式。

句法:

public final String format(Date date)

参数:该方法采用Date对象类型的一个参数date,指的是要产生字符串输出的日期。

返回值:该方法以“mm/dd/yyyy”的字符串格式返回日期或时间

实现:在这个Java示例中,输入的时间 user 使用 AM/PM 标记从 24 小时格式转换为12小时格式。

Java
// Java program to convert 24 hour
// time format to 12 hour format
 
// Importing specific date class libraries
import java.util.Date;
import java.text.SimpleDateFormat;
 
public class GFG {
   
  // Main driver method
    public static void main(String[] args)
    {
        // Getting the current current time
        Date date = new Date();
 
       
        System.out.println("Current Time is : " + date);
 
        // set format in 12 hours
        SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa");
        // hh = hours in 12hr format
        // mm = minutes
        // aa = am/pm
 
        String time = formatTime.format(
            date); // changing the format of 'date'
 
        // display time as per format
        System.out.println(
            "Current Time in AM/PM Format is : " + time);
    }
}


Java
// Java program to convert 24 hour
// time format to 12 hour format
 
// Importing generic java libraries
import java.util.*;
 
class GFG {
 
    // Function converting entered values to String type
    static void convertTime(String time)
    {
        String format;
 
        // Parsing hours, minutes and seconds in array
        String[] arr = time.split(":");
 
        // Converting hours into integer
        int hh = Integer.parseInt(arr[0]);
 
        if (hh > 12) {
            hh = hh - 12;
            format = "PM";
        }
        else if (hh == 00) {
            hh = 12;
            format = "AM";
        }
        else if (hh == 12) {
            hh = 12;
            format = "PM";
        }
        else {
            format = "AM";
        }
 
        // Converting hh to String and
        // padding it with 0 on left side
        String hour = String.format("%02d", hh);
        String minute = arr[1];
        String second = arr[2];
 
        // Printing formatted time
        System.out.print("Time in 12-hour format is : ");
        System.out.print(hour + ":" + minute + ":" + second
                         + " " + format);
    }
 
    // Main driver code
    public static void main(String[] args)
    {
        // Taking input from the user via Scanner class
        Scanner sc = new Scanner(System.in);
 
        // Asking from user to enter
        // time in 24 format
        System.out.println(
            "Enter the time in 24-hour format : ");
 
        /* User: Remember to enter time in below format
                 Enter in this format - '14:02:45' */
        String time = sc.nextLine();
 
        // Passing time as entered above as parameter
        // wher efunrion isn
        convertTime(time);
    }
}



输出
Current Time is : Mon Oct 26 08:34:53 UTC 2020
Current Time in AM/PM Format is : 08.34 AM

方法二:不使用任何特殊的Java类

此处仅使用内置方法,如下以表格形式列出,以及它们在转换中的作用。时间与 24 小时格式的 12 格式标准中的 AM/PM 标记一致,甚至无需导入 Date 类。

Method nameAction performed
split()It is used to split the string
time.split(“:”)This syntax in our program means that the split function is applied to string ‘time’  and it is splitting the string by ‘ : ‘  character which is passed inside the function.
format( )It is used to change the format of a string.

执行:

Java

// Java program to convert 24 hour
// time format to 12 hour format
 
// Importing generic java libraries
import java.util.*;
 
class GFG {
 
    // Function converting entered values to String type
    static void convertTime(String time)
    {
        String format;
 
        // Parsing hours, minutes and seconds in array
        String[] arr = time.split(":");
 
        // Converting hours into integer
        int hh = Integer.parseInt(arr[0]);
 
        if (hh > 12) {
            hh = hh - 12;
            format = "PM";
        }
        else if (hh == 00) {
            hh = 12;
            format = "AM";
        }
        else if (hh == 12) {
            hh = 12;
            format = "PM";
        }
        else {
            format = "AM";
        }
 
        // Converting hh to String and
        // padding it with 0 on left side
        String hour = String.format("%02d", hh);
        String minute = arr[1];
        String second = arr[2];
 
        // Printing formatted time
        System.out.print("Time in 12-hour format is : ");
        System.out.print(hour + ":" + minute + ":" + second
                         + " " + format);
    }
 
    // Main driver code
    public static void main(String[] args)
    {
        // Taking input from the user via Scanner class
        Scanner sc = new Scanner(System.in);
 
        // Asking from user to enter
        // time in 24 format
        System.out.println(
            "Enter the time in 24-hour format : ");
 
        /* User: Remember to enter time in below format
                 Enter in this format - '14:02:45' */
        String time = sc.nextLine();
 
        // Passing time as entered above as parameter
        // wher efunrion isn
        convertTime(time);
    }
}

输出: