Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.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_Android_String - Fatal编程技术网

Java 如何将字符串转换为相应的日期、月份和日期

Java 如何将字符串转换为相应的日期、月份和日期,java,android,string,Java,Android,String,我有 String a= "may 22-may 28 Free" 这来自于反应。 从服务到服务如何改变成这样 Tue,5.22- thur,5.28 注意:最后给出了两个空格,即在日期之后,然后是一个免费的文本,所以忽略这一点,只考虑回复的日期,怎么办?有什么想法吗 DateFormatter formatter = new SimpleDateFormat("dd-MM-yyyy:HH:mm:SS"); convertedDate = (Date)

我有

    String a= "may 22-may 28  Free"  
这来自于反应。 从服务到服务如何改变成这样

    Tue,5.22- thur,5.28 
注意:最后给出了两个空格,即在日期之后,然后是一个免费的文本,所以忽略这一点,只考虑回复的日期,怎么办?有什么想法吗

    DateFormatter formatter = new SimpleDateFormat("dd-MM-yyyy:HH:mm:SS");
    convertedDate = (Date) formatter.parse(a);
但我无法实现这一点,因为我无法根据预期的输出进行分割和正确显示
就像这个星期二,5点22分-星期四,5点28分。
如何做到这一点


提前感谢

对于您拥有的字符串和所需的返回类型,没有内置解析器可用。您可以检查此处支持的所有可用日期和时间模式

您必须编写自己的解析器

您可以尝试先用“-”和“”(进一步空格)拆分字符串。然后我相信你可以很容易地得到你的格式

简单的代码是:

字符串a=“5月22日5月28日免费”;字符串[]数组=新字符串[2]; 数组=a.split('-')

//您将把数组设置为{'may 22','may 28 Free'}-让我们看一看 第一个数组值,即数组[0]

字符串日期[]=新字符串[2];日期=数组[0]。拆分(“”)

//您将把日期数组设置为{'may','22'}

您可以创建一个switch case来返回月份('may')字符串的正确月份号,使用日期('22')

要获得一周中的哪一天,请使用以下代码:(确保您也有年份)

Calendar c=Calendar.getInstance();c、 设定时间(您的日期); int dayOfWeek=c.get(日历。一周中的一天)

这是我能推荐的最简单的代码。你当然可以进一步即兴创作。此外,每当您得到一个具有2个以上属性的数组时(根据给定的示例),最后一个字符串是“Free”,您可以在那里停止执行


希望有帮助

对于您拥有的字符串和所需的返回类型,没有内置解析器可用。您可以检查此处支持的所有可用日期和时间模式

您必须编写自己的解析器

您可以尝试先用“-”和“”(进一步空格)拆分字符串。然后我相信你可以很容易地得到你的格式

简单的代码是:

字符串a=“5月22日5月28日免费”;字符串[]数组=新字符串[2]; 数组=a.split('-')

//您将把数组设置为{'may 22','may 28 Free'}-让我们看一看 第一个数组值,即数组[0]

字符串日期[]=新字符串[2];日期=数组[0]。拆分(“”)

//您将把日期数组设置为{'may','22'}

您可以创建一个switch case来返回月份('may')字符串的正确月份号,使用日期('22')

要获得一周中的哪一天,请使用以下代码:(确保您也有年份)

Calendar c=Calendar.getInstance();c、 设定时间(您的日期); int dayOfWeek=c.get(日历。一周中的一天)

这是我能推荐的最简单的代码。你当然可以进一步即兴创作。此外,每当您得到一个具有2个以上属性的数组时(根据给定的示例),最后一个字符串是“Free”,您可以在那里停止执行


希望有帮助

您可以使用下面的代码进行尝试

public static void main(String[] args) {

    String a= "may 22-may 28"  ;
    String[]arr = a.split("-");
    String date1 = arr[0];
    String date2 = arr[1];

    String result = getFormattedDate(date1) +"- " + getFormattedDate(date2);
    System.out.println(result); 
    //Output is Fri,5.22- Thu,5.28

}

public static String getFormattedDate(String inputDate){
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
    SimpleDateFormat sdf2 = new SimpleDateFormat("EE,M.dd");
    String result = "";
    Date d1 = null;
    try {
        d1 = sdf.parse(inputDate);
        result = sdf2.format(d1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return result;
}

您可以使用下面的代码进行尝试

public static void main(String[] args) {

    String a= "may 22-may 28"  ;
    String[]arr = a.split("-");
    String date1 = arr[0];
    String date2 = arr[1];

    String result = getFormattedDate(date1) +"- " + getFormattedDate(date2);
    System.out.println(result); 
    //Output is Fri,5.22- Thu,5.28

}

public static String getFormattedDate(String inputDate){
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
    SimpleDateFormat sdf2 = new SimpleDateFormat("EE,M.dd");
    String result = "";
    Date d1 = null;
    try {
        d1 = sdf.parse(inputDate);
        result = sdf2.format(d1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return result;
}

我想你想要这样的东西:

    final String a = "dec 22-may 28";
    final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
    final SimpleDateFormat stringToDate = new SimpleDateFormat("MMM dd");
    final SimpleDateFormat dateToString = new SimpleDateFormat("EE,M.dd");
    final String[] dates = a.split("-");

    // First
    Date convertedDate = stringToDate.parse(dates[0]);
    final Calendar c1 = Calendar.getInstance();
    c1.setTime(convertedDate);
    c1.set(Calendar.YEAR, currentYear);
    System.out.println(c1.getTime());
    System.out.println(dateToString.format(c1.getTime()));

    // Second
    convertedDate = stringToDate.parse(dates[1]);
    final Calendar c2 = Calendar.getInstance();
    c2.setTime(convertedDate);
    c2.set(Calendar.YEAR, currentYear);
    if (c2.before(c1)) {
        c2.set(Calendar.YEAR, currentYear + 1);
    }
    System.out.println(c2.getTime());
    System.out.println(dateToString.format(c2.getTime()));

请记住,我使用了dec-may,因为这是我能想到的最糟糕的情况。还请注意,我将第一个日期作为当前年份,第二个日期可能是明年(如本例所示)。

我认为您需要这样的日期:

    final String a = "dec 22-may 28";
    final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
    final SimpleDateFormat stringToDate = new SimpleDateFormat("MMM dd");
    final SimpleDateFormat dateToString = new SimpleDateFormat("EE,M.dd");
    final String[] dates = a.split("-");

    // First
    Date convertedDate = stringToDate.parse(dates[0]);
    final Calendar c1 = Calendar.getInstance();
    c1.setTime(convertedDate);
    c1.set(Calendar.YEAR, currentYear);
    System.out.println(c1.getTime());
    System.out.println(dateToString.format(c1.getTime()));

    // Second
    convertedDate = stringToDate.parse(dates[1]);
    final Calendar c2 = Calendar.getInstance();
    c2.setTime(convertedDate);
    c2.set(Calendar.YEAR, currentYear);
    if (c2.before(c1)) {
        c2.set(Calendar.YEAR, currentYear + 1);
    }
    System.out.println(c2.getTime());
    System.out.println(dateToString.format(c2.getTime()));

请记住,我使用了dec-may,因为这是我能想到的最糟糕的情况。另请注意,我将第一个日期作为当前年份,第二个日期可能是明年(如本例中所示)。

第二个数字应该是同一个月?不@m0skit0从服务中假设,如果是7月31日至8月2日,它会变化,年份是什么?现在的?同样对于年间日期间隔,如何知道起始年份?是上一个还是当前(例如12月31日至1月2日)?本年度@m0skit0@user3848333在添加澄清时编辑您的问题,而不是作为评论发布。请注意问题标签下方左上方的
edit
链接。第二个号码应该是同一个月?如果是7月31日至8月2日,则不会@m0skit0假设是从服务开始的,它会变化,年份是什么?现在的?同样对于年间日期间隔,如何知道起始年份?是上一个还是当前(例如12月31日至1月2日)?本年度@m0skit0@user3848333在添加澄清时编辑您的问题,而不是作为评论发布。请注意左上方问题标签下方的
edit
链接。