Java-时间日期格式转换

时间日期格式转换

Time Limit: 1000 ms  Memory Limit: 65536 KiB
Problem Description
而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制
12小时制的表示方法是”05:30:00pm”。注意12:00:00pm表示中午12点,而12:00:00am 表示凌晨12点。
 第一行为一个整数T(T<=20),代表总共需要转换的时间日期字符串的数目。
<span 18px;="" \"="">接下来的总共T行,每行都是一个需要转换的时间日期字符串。
Output
 分行输出转换之后的结果
Sample Input
2
2010/11/20-12:12:12
1970/01/01-00:01:01
Sample Output
11/20/2010-12:12:12pm
01/01/1970-12:01:01am

Hint


import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Locale;  
import java.util.Scanner;  
  
public class Main {  
    public static void main(String[] args) throws ParseException {  
        Scanner cin = new Scanner(System.in);  
        int t = cin.nextInt();  
        for(int i = 0;i < t;i++){  
            String strDate = cin.next();  
            SimpleDateFormat date_format = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");  
            Date date = date_format.parse(strDate);  
            SimpleDateFormat date_format1 = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ssa",new Locale("US"));//a是用来判定AM还是PM,Locale 设置时区  
            System.out.println(date_format1.format(date).toLowerCase());  
        }  
    }  
}  

import java.util.*;  
import java.lang.*;  
  
public class Main{  
    public static void main(String[] args){  
        Scanner in = new Scanner(System.in);  
        int t = in.nextInt();  
        in.nextLine();  
        while(t-- >= 0){  
            String s = in.nextLine();  
            int dex = s.indexOf("-");    //indexof查找字符所在位置
            String date = s.substring(0, dex);  //分割字符串
            String time = s.substring(dex+1);  
            String[] d = date.split("/");  
            String[] tt = time.split(":");  
            String flag = new String();  
            int y = Integer.parseInt(tt[0]);  //小时
            int m = Integer.parseInt(tt[1]);  //分钟
            int sc = Integer.parseInt(tt[2]);  //秒
            if(y >= 12)  
                flag = "pm";  
            else  
                flag = "am";  
            if(y > 12)  
                y -= 12;  
            else if(y == 0)  
                y=12;  
            System.out.printf("%s/%s/%s-%02d:%02d:%02d%s\n", d[1], d[2], d[0], y, m, sc, flag);  
              
        }  
    }  
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值