C++程序 从12小时制转换为24小时制

C++程序 从12小时制转换为24小时制

给定12小时AM / PM格式的时间,将其转换为军事(24小时)时间。

注意:午夜在12小时制钟表上是12:00:00 AM,在24小时制钟表上是00:00:00。中午在12小时制钟表上是12:00:00 PM,在24小时制钟表上是12:00:00。

例:

输入 : 一个字符串,其中包含12小时制格式的时间(hh:mm:ss AM或hh:mm:ss PM),其中01<=hh<=12或01<=mm,ss<=59
输出 :以24小时格式转换并打印给定时间,其中00<=hh<=23

输入 : 07:05:45PM
输出 : 19:05:45

// C++程序将12小时制转换为24小时制
//格式
#include<iostream>
using namespace std;
 
void print24(string str)
{
    //获取小时数
    int h1 = (int)str[1] - '0';
    int h2 = (int)str[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    //如果时间是“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];
        }
    }
 
    //如果时间是“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];
        }
    }
}
 
//驱动器代码
int main()
{
   string str = "07:05:45PM";
   print24(str);
   return 0;
}
// Java程序将12小时制格式转换为24小时格式
import java.io.*;
public class GFG
{
static void print24(String str)
{
    //获取小时数
    int h1 = (int)str.charAt(1) - '0';
    int h2 = (int)str.charAt(0) - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    //如果时间是“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));
        }
    }
 
    //如果时间是“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));
        }
    }
}
 
//驱动器代码
public static void main(String[] args)
{
    String str = "07:05:45PM";
    print24(str);
}
}
 
//此代码由Anant Agarwal贡献。
# Python3程序,将12小时制转换为24小时制
def print24(s):
 
    #获取小时
    h1 = ord(s[1]) - ord('0')
    h2 = ord(s[0]) - ord('0')
    hh = (h2 * 10 + h1 % 10)
  
    #如果时间是“上午”
    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 = '')
  
    #如果时间是“下午”
    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 = '')
             
#驱动程序          
if __name__=="__main__":
 
   s = "07:05:45PM"
    
   print24(s)
    
#该代码由rutvik_56提供
//C#程序,将12小时制转换为24小时制
using System;
 
class GFG
{
     
static void print24(String str)
{
    //获取小时
    int h1 = (int)str[1] - '0';
    int h2 = (int)str[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
 
    //如果时间是“上午”
    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]);
        }
    }
 
    //如果时间是“下午”
    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]);
        }
    }
}
 
//驱动程序
public static void Main(String[] args)
{
    String str = "07:05:45PM";
    print24(str);
}
}
 
//该代码由Rajput-Ji提供
<script>
// JavaScript程序,将12小时制转换为24小时制
  
 
  function print24(str)
{
    //获取小时
    var h1 = Number(str[1] - '0');
    var h2 = Number(str[0] - '0');
    var hh = (h2 * 10 + h1 % 10);
  
    //如果时间是“上午”
    if (str[8] == 'A')
    {
        if (hh == 12)
        {
            document.write("00");
            for (var i = 2; i <= 7; i++)
                document.write(str[i]);
        }
        else
        {
            for (var i = 0;<= 7; i++)
                document.write(str[i]);
        }
    }
  
    //如果时间是“下午”
    else
    {
        if (hh == 12)
        {
            document.write("12");
            for (var i = 2; i <= 7; i++)
                document.write(str[i]);
        }
        else
        {
            hh = hh + 12;
            document.write(hh);
            for (var i = 2; i <= 7; i++)
                document.write(str[i]);
        }
    }
}
  
//驱动程序
 
    var str = "07:05:45PM";
    print24(str);
 
//该代码由bunnyram19提供。
</script>```  

输出

19:05:45

时间复杂度: O(1)

辅助空间 : O(1)

方法2:使用Java Date

#include <iostream>
#include <ctime>
using namespace std;
 
string englishTime(string input)
{
   
    // Format of the date defined in the input String
    struct tm tm;
    strptime(input.c_str(), "%I:%M:%S %p", &tm);
 
    // Changing the format of date and storing it in string
    char output[9];
    strftime(output, sizeof(output), "%H:%M:%S", &tm);
    return string(output);
}
 
// Driver code
int main() {
    cout << englishTime("07:05:45 PM") << endl;
    return 0;
}
// Java程序实现以上方法
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class GFG {
    public static String englishTime(String input)
        throws ParseException
    {
 
        // 将输入字符串中指定格式的日期字符串格式化并转换成Date对象
        DateFormat dateFormat
            = new SimpleDateFormat("hh:mm:ss aa");
       
        // 将24小时制转化成指定格式的日期字符串
        DateFormat format
            = new SimpleDateFormat("HH:mm:ss");
        Date time = null;
        String output = "";
       
        // 将形如"07:05:45 PM"格式的字符串转化为Date对象
        time = dateFormat.parse(input);
       
        // 转化日期并存储字符串
        output = format.format(time);
        return output;
    }
 
    // Driver code
    public static void main(String[] arg)
        throws ParseException
    {
        System.out.println(englishTime("07:05:45 PM"));
    }
}
# Python程序实现以上方法
import datetime
 
 
def englishTime(input):
    # 将输入字符串中指定格式的日期字符串格式化并转换成Date对象
    dateFormat = datetime.datetime.strptime(input, "%I:%M:%S %p")
 
    # 将24小时制转化成指定格式的日期字符串
    format = datetime.datetime.strftime(dateFormat, "%H:%M:%S")
    return format
 
 
# Driver Code
print(englishTime("07:05:45 PM"))
using System;
using System.Globalization;
 
class GFG {
  public static string englishTime(string input)
  {
 
    // 将字符串中格式为“hh:mm:ss tt”的时间和格式存储在DateTime变量中
    DateTime time;
    CultureInfo provider = CultureInfo.InvariantCulture;
    DateTime.TryParseExact(input, "hh:mm:ss tt", provider, DateTimeStyles.None, out time);
 
    // 将日期时间格式转换为指定格式的字符串
    DateTimeFormatInfo dtfi = CultureInfo.InvariantCulture.DateTimeFormat;
    string output = time.ToString("HH:mm:ss", dtfi);
    return output;
  }
 
  // Driver Code
  static void Main(string[] args) {
    Console.WriteLine(englishTime("07:05:45 PM"));
  }
}
// JavaScript程序实现以上方法
function englishTime(input) {
  // 用输入字符串创建一个Date对象
  let date = new Date("1970-01-01 " + input);
 
  // 将日期对象格式化为24小时制
  let format = date.toLocaleTimeString([], { hour12: false });
 
  return format;
}
 
// Driver Code
console.log(englishTime("07:05:45 PM"));```  

输出结果

19:05:45

时间复杂度 : O(1)

辅助空间复杂度 : O(1)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C++ 示例