简体   繁体   English

在java中将美国日期转换为EU日期

[英]Converting US date to EU date in java

Without using a "dateformat" or something along those lines, I am trying to change a string of US date "mm/dd/yyyy" to an EU format "dd/mm/yyyy". 如果不使用“dateformat”或类似的东西,我试图将美国日期“mm / dd / yyyy”的字符串更改为EU格式“dd / mm / yyyy”。 I am to do this using strings and string methods. 我是使用字符串和字符串方法来做到这一点。 I feel like I am very close to this but cannot quite figure it out. 我觉得我非常接近这一点,但无法弄明白。 This is my code: 这是我的代码:

import java.util.Scanner;

public class DateChange {

    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        String usDate, euDate, day, month;
        int year;
        System.out.println("Enter a date in the form month/day/year:");
        usDate = kbd.nextLine();
        month = usDate.substring(0, '/');
        day = usDate.substring('/', '/');
        year = usDate.lastIndexOf('/');
        euDate = day + "." + month + "." + year;
        System.out.println("Your date in European form is:");
        System.out.println(euDate);
    }

}

This is the error I am getting: 这是我得到的错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 47
    at java.lang.String.substring(String.java:1963)
    at DateChange.main(DateChange.java:12)

Why not split the input string using '/' as separator using the method String.split . 为什么不使用方法String.split使用'/'作为分隔符拆分输入字符串。 It returns an array of strings. 它返回一个字符串数组。 Just swap the first two elements of the array. 只需交换数组的前两个元素。

String usdate = "12/27/2015";
String[] arr = usdate.split("/");
String eudate = arr[1] + "/" + arr[0] + "/" + arr[2];

The problem is that java.String.substring takes two int values, not char values. 问题是java.String.substring需要两个int值,而不是char值。 The int value of ' / ' is 47, so substring is taking 47 as an input, not the indexOf ' / '. '/'的int值为47,因此substring以47为输入,而不是indexOf '/'。

If you want to solve this without using the java library, use the following code: 如果要在不使用Java库的情况下解决此问题,请使用以下代码:

import java.util.*;

public class DateChange {
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        String usDate, euDate, day = "", month = "", year = "";
        System.out.println("Enter a date in the form month/day/year:");
        usDate = kbd.nextLine();
        kbd.close();
        month = usDate.substring(0, usDate.indexOf("/"));
        day = usDate.substring(usDate.indexOf("/")+1, usDate.lastIndexOf("/"));
        year = usDate.substring(usDate.lastIndexOf("/")+1, usDate.length());
        euDate = day + "." + month + "." + year;
        System.out.println("Your date in European form is:");
        System.out.println(euDate);
    }
}
Scanner kbd = new Scanner(System.in);
        String usString = kbd.nextLine();
        String[] stringArray = usString.split("(\\/|\\.)");
        String month = stringArray[0];
        String day = stringArray[1];
        String year = stringArray[2];
        System.out.println("m"+month+"d"+day+"y"+year);

This code shows how to use RegEx to split the sting into a string array which can then be re assembled into EU format without the DateFormat class. 此代码显示如何使用RegEx将sting拆分为字符串数组,然后可以在没有DateFormat类的情况下将其重组为EU格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM