Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java上获得奇怪的输出(新代码)_Java_Output - Fatal编程技术网

在Java上获得奇怪的输出(新代码)

在Java上获得奇怪的输出(新代码),java,output,Java,Output,输入是2013年4月3日,输出应该是2013年4月3日,但是我一直得到的输出是2013年4月20日 import java.util.Scanner; public class DateConversion { public static void main( String [] args ) { Scanner sc = new Scanner ( System.in ); System.out.println("Enter the date: ");

输入是2013年4月3日,输出应该是2013年4月3日,但是我一直得到的输出是2013年4月20日

 import java.util.Scanner;
    public class DateConversion {
    public static void main( String [] args ) {


    Scanner sc = new Scanner ( System.in );


    System.out.println("Enter the date: ");
    String temp = sc.nextLine();

    String day =temp.substring(6);
    String month = temp.substring(0, 4);
    String year = temp.substring(9, 12);

    System.out.println(day + "-" + month + "-" + year);

    sc.close();


     }
   }

您的
day
子字符串只包含一个值(因此它从输入的第六个字符开始,一直到输入的末尾)。我将使用
String.split
来获取代币。像

String temp = "April 3,2013";
String[] tokens = temp.split("[\\s,]");
System.out.printf("%s-%s-%s%n", tokens[1], tokens[0], tokens[2]);

String类
substring
方法将字符串从startposition拆分到endposition-1
substring(start,end-1)


顺便说一句,你不应该在中关闭
系统。
Scanner sc = new Scanner ( System.in );


    System.out.println("Enter the date: ");
    String temp = sc.nextLine();

    String day =temp.substring(6,7);
    String month = temp.substring(0, 5);
    String year = temp.substring(8, 12);

    System.out.println(day + "-" + month + "-" + year); //3-April-2013

    sc.close();