📌  相关文章
📜  程序将时间从12小时转换为24小时格式

📅  最后修改于: 2021-05-30 11:44:26             🧑  作者: Mango

给定时间为12小时AM / PM格式,请将其转换为军事(24小时)时间。
注意:午夜是12小时制的12:00:00 AM和24小时制的00:00:00。中午是12小时制的12:00:00 PM和24小时制的12:00:00

例子:

Input : A single string containing a time in 12-hour 
clock format(hh:mm:ss AM or hh:mm:ss PM
        where 01 <= hh <= 12 or 01 <= mm,ss <= 59 
Output :Convert and print the given time in 24-hour format,
where 00 <= hh <= 23

Input : 07:05:45PM
Output : 19:05:45
C++
// C++ program to convert 12 hour to 24 hour
// format
#include
using namespace std;
 
void print24(string str)
{
    // Get hours
    int h1 = (int)str[1] - '0';
    int h2 = (int)str[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    // If time is in "AM"
    if (str[8] == 'A')
    {
        if (hh == 12)
        {
            cout << "00";
            for (int i=2; i <= 7; i++)
                cout << str[i];
        }
        else
        {
            for (int i=0; i <= 7; i++)
                cout << str[i];
        }
    }
 
    // If time is in "PM"
    else
    {
        if (hh == 12)
        {
            cout << "12";
            for (int i=2; i <= 7; i++)
                cout << str[i];
        }
        else
        {
            hh = hh + 12;
            cout << hh;
            for (int i=2; i <= 7; i++)
                cout << str[i];
        }
    }
}
 
// Driver code
int main()
{
   string str = "07:05:45PM";
   print24(str);
   return 0;
}


Java
// Java program to convert 12 hour
// format to  24 hour format
class GFG
{
static void print24(String str)
{
    // Get hours
    int h1 = (int)str.charAt(1) - '0';
    int h2 = (int)str.charAt(0) - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    // If time is in "AM"
    if (str.charAt(8) == 'A')
    {
        if (hh == 12)
        {
            System.out.print("00");
            for (int i = 2; i <= 7; i++)
                System.out.print(str.charAt(i));
        }
        else
        {
            for (int i = 0; i <= 7; i++)
                System.out.print(str.charAt(i));
        }
    }
 
    // If time is in "PM"
    else
    {
        if (hh == 12)
        {
            System.out.print("12");
            for (int i = 2; i <= 7; i++)
                System.out.print(str.charAt(i));
        }
        else
        {
            hh = hh + 12;
            System.out.print(hh);
            for (int i = 2; i <= 7; i++)
                System.out.print(str.charAt(i));
        }
    }
}
 
// Driver code
public static void main (String[] args)
{
    String str = "07:05:45PM";
    print24(str);
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to convert 12
# hour to 24 hour format
def print24(s):
 
    # Get hours
    h1 = ord(s[1]) - ord('0')
    h2 = ord(s[0]) - ord('0')
    hh = (h2 * 10 + h1 % 10)
  
    # If time is in "AM"
    if (s[8] == 'A'):
        if (hh == 12):
            print('00', end = '')
             
            for i in range(2, 8):
                print(s[i], end = '')
 
        else:
            for i in range(0, 8):
                print(s[i], end = '')
  
    # If time is in "PM"
    else:
        if (hh == 12):
            print("12", end = '')
             
            for i in range(2, 8):
                print(s[i], end = '')
         
        else:
            hh = hh + 12;
            print(hh, end = '')
             
            for i in range(2, 8):
                print(s[i], end = '')
             
# Driver code          
if __name__=="__main__":
 
   s = "07:05:45PM"
    
   print24(s)
    
# This code is contributed by rutvik_56


C#
// C# program to convert 12 hour
// format to 24 hour format
using System;
 
class GFG
{
     
static void print24(String str)
{
    // Get hours
    int h1 = (int)str[1] - '0';
    int h2 = (int)str[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    // If time is in "AM"
    if (str[8] == 'A')
    {
        if (hh == 12)
        {
            Console.Write("00");
            for (int i = 2; i <= 7; i++)
                Console.Write(str[i]);
        }
        else
        {
            for (int i = 0; i <= 7; i++)
                Console.Write(str[i]);
        }
    }
 
    // If time is in "PM"
    else
    {
        if (hh == 12)
        {
            Console.Write("12");
            for (int i = 2; i <= 7; i++)
                Console.Write(str[i]);
        }
        else
        {
            hh = hh + 12;
            Console.Write(hh);
            for (int i = 2; i <= 7; i++)
                Console.Write(str[i]);
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "07:05:45PM";
    print24(str);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


Java
// Java program for the above approach
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
class GFG {
    public static String englishTime(String input)
        throws ParseException
    {
 
        // Format of the date defined in the input String
        DateFormat dateFormat
            = new SimpleDateFormat("hh:mm:ss aa");
       
        // Change the pattern into 24 hour formate
        DateFormat formate
            = new SimpleDateFormat("HH:mm:ss");
        Date time = null;
        String output = "";
       
        // Converting the input String to Date
        time = dateFormat.parse(input);
       
        // Changing the format of date
        // and storing it in
        // String
        output = formate.format(time);
        return output;
    }
 
    // Driver Code
    public static void main(String[] arg)
        throws ParseException
    {
        System.out.println(englishTime("07:05:45 PM"));
    }
}


输出:

19:05:45

方法2:使用Java日期

Java

// Java program for the above approach
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
class GFG {
    public static String englishTime(String input)
        throws ParseException
    {
 
        // Format of the date defined in the input String
        DateFormat dateFormat
            = new SimpleDateFormat("hh:mm:ss aa");
       
        // Change the pattern into 24 hour formate
        DateFormat formate
            = new SimpleDateFormat("HH:mm:ss");
        Date time = null;
        String output = "";
       
        // Converting the input String to Date
        time = dateFormat.parse(input);
       
        // Changing the format of date
        // and storing it in
        // String
        output = formate.format(time);
        return output;
    }
 
    // Driver Code
    public static void main(String[] arg)
        throws ParseException
    {
        System.out.println(englishTime("07:05:45 PM"));
    }
}

输出:

19:05:45
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”