time date format conversion

Problem Description
The common format for dates is "year-year-year/month-month/day-day" in China or "yyyy/mm/dd" written in English abbreviations. The start date of this programming competition "2010/11/20" is a date that conforms to this format,
while the date format used in North America is "mm/dd/yyyy" or "mm/dd/yyyy", such as "2010/ 11/20" is changed to this format, which corresponds to "11/20/2010". For the time format, there are often 12-hour and 24-
hour representations. The 24-hour system uses 0-24 to represent the 24 hours in a day, while the 12-hour system only uses 1-12 to represent the hour, plus am /pm means morning or afternoon. For example, "17:30:00" is used to indicate time in 24-hour format, and the corresponding
12-hour format is "05:30:00pm". Note that 12:00:00pm means 12 noon and 12:00:00am means 12 am.
For a given string representing date and time using "yyyy/mm/dd" plus 24-hour format (connected with dashes "-"), please program it to convert it to "mm/dd/yyyy" plus String in 12-hour format.
The first line of Input
is an integer T (T<=20), which represents the total number of time and date strings that need to be converted.
The next T lines in total, each line is a time and date string that needs to be converted.
Output
branch output result after conversion
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

import java.util.*;
import java.text.*;

public class Main {

    public static void main(String[] args) throws ParseException{
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        sc.nextLine();//必须承接换行

        while(t-- != 0) {
            String s = sc.nextLine();

            SimpleDateFormat in = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");//24小时制小时为HH
            SimpleDateFormat out = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ssa", new Locale("US"));//12小时制小时为hh,a为上午下午,new Locale("US")美国时区
            Date date = null;
            date = in.parse(s);//将字符串换成Date日期型

            s = out.format(date);//将date按照out格式转换

            System.out.println(s.toLowerCase());//AM,PM为大写

        }
        sc.close();
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839556&siteId=291194637