世界上不同国家有不同的写日期的习惯。比如美国人习惯写成“月-日-年”,而中国人习惯写成“年-月-日”。下面请你写个程序,自动把读入的美国格式的日期改写成中国习惯的日期。
输入格式:
输入在一行中按照“mm-dd-yyyy”的格式给出月、日、年。题目保证给出的日期是1900年元旦至今合法的日期。
输出格式:
在一行中按照“yyyy-mm-dd”的格式给出年、月、日。
输入样例:
03-15-2017
输出样例:
2017-03-15
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
String str=scan.next();
String year=str.substring(6,10);// //字符串截取,返回一个新的字符串
String month=str.substring(0,2);//包括起始数字,不包括末尾数字
String day=str.substring(3,5);
System.out.println(year+"-"+month+"-"+day);
scan.close();
}
}
substring的用法
substring(int beginIndex, int endIndex)
//begIndex 起始数字 从0开始 包括0
//endIndex 末尾数字 不包括