年月日格式转换练习
目标:
输入n组( 0< n <=100 )8位或6位数字。将8位数转换为xxxx-xx-xx的格式,将6位数转换为xxxx-xx的格式。
举例:
输入:19970102199701
输出:1997-01-02
1997-01
java:
import java.util.*;
public class 年月转换 {
public static void main(String[] args){
int[] ymd=new int[500];
System.out.println("enter the date,please: ");
Scanner in=new Scanner(System.in);
for(int i=0;i<500;i++){
ymd[i]=in.nextInt();
if((int)(ymd[i]/1000000)==0){//为XXXX-XX
if(ymd[i]%100/10==0){ //月份为0X月
System.out.println((int)ymd[i]/100+"-0"+(int)ymd[i]%10);
}else //0x-xx
System.out.println((int)ymd[i]/100+"-"+(int)ymd[i]%100);
}else{//为XXXX-XX-XX
if(ymd[i]%10000/1000==0){ //月份为0X月
if(ymd[i]%100/10==0){ //天为0X天
System.out.println((int)ymd[i]/10000+"-0"+(int)ymd[i]%1000/100+"-0"+ymd[i]%10);
}else //0x-xx
System.out.println((int)ymd[i]/10000+"-0"+(int)ymd[i]%1000/100+"-"+ymd[i]%100);
}else if(ymd[i]%100/10==0){//xx0x
System.out.println((int)ymd[i]/10000+"-"+(int)ymd[i]%10000/100+"-0"+ymd[i]%10);
}
else //xxxX天
System.out.println((int)ymd[i]/10000+"-"+(int)ymd[i]%10000/100+"-"+ymd[i]%100);
}
}
}
}