[英]How to convert a string to corresponding day,month and date
Say for eg: I have 比如说:我有
String a= "may 22-may 28 Free"
which comes from response. 来自回应。 from service how to change that to like this?
从服务如何变成这样?
Tue,5.22- thur,5.28
Note: two space been given at last ie after date and then a text as free, so neglecting that and considering only date from response,how to do? 注意:最后给了两个空格,即日期之后,然后是一个自由文本,因此忽略该空格,只考虑响应中的日期,怎么办? any idea?
任何想法?
DateFormatter formatter = new SimpleDateFormat("dd-MM-yyyy:HH:mm:SS");
convertedDate = (Date) formatter.parse(a);
but this i couldn't achieve because i couldn't split and properly display based on the 但这是我无法实现的,因为我无法根据
expected output 预期产量
as like this Tue,5.22- thur,5.28. 就像这个星期二5.22至5.28。
How to achieve this? 如何实现呢?
Thanks in advance 提前致谢
The is no in-built Parser available for the String you have and the return type you want. 没有内置的解析器可用于您拥有的String和所需的返回类型。 You can check all the available Date and Time patterns supported here http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
您可以在此处检查所有受支持的日期和时间模式, 网址为http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
You shall have to write your own parser. 您将必须编写自己的解析器。
You can try to split the String with '-' first and ' ' (space further). 您可以尝试先用'-'和''(更远的空格)分割字符串。 And then I am sure you can easily get your format.
然后,我相信您可以轻松获取格式。
Simple code would be : 简单的代码是:
String a= "may 22-may 28 Free" ;
字符串a =“ 5月22日至5月28日免费”; String[] array = new String[2];
String [] array = new String [2]; array = a.split('-');
数组= a.split('-');
// You will hv the array set to {'may 22','may 28 Free'} - Let's take first array value, ie array[0]
//将数组设置为{'may 22','may 28 Free'}-让我们获取第一个数组值,即array [0]
String date[] = new String[2];
字符串date [] =新的String [2]; date = array[0].split('');
日期= array [0] .split('');
// You will have date array set to {'may', '22'}
//您将日期数组设置为{'may','22'}
You can create a switch-case to return proper month number for Month('may') strings, use the date ('22') as it is. 您可以创建一个开关箱来为Month('may')字符串返回正确的月份号,并按原样使用日期('22')。
For getting the Day of the week - use below code : (Make Sure you have Year too) 要获取星期几,请使用以下代码:(确保您也拥有Year)
Calendar c = Calendar.getInstance();
日历c = Calendar.getInstance(); c.setTime(yourDate);
c.setTime(yourDate); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
This is the simplest of the code I can suggest. 这是我建议的最简单的代码。 You can surely improvise it further.
您当然可以进一步即兴创作。 Also whenever you get an array with more than 2 attributes (as per given example) the last string is 'Free' and you can stop your execution there.
同样,每当您获得一个具有2个以上属性的数组时(按照给定的示例),最后一个字符串为“ Free”,您可以在此处停止执行。
Hope it helps! 希望能帮助到你!
I think you want something like this: 我想你想要这样的东西:
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()));
Keep in mind that I used dec-may because it's worst case I can think of. 请记住,我使用dec-may,因为这是我能想到的最坏的情况。 Also note I take first date as current year, second date might be next year (like in this case).
另请注意,我将第一个日期设为当前年份,第二个日期可能是明年(如本例所示)。
You can try it using below code. 您可以使用以下代码尝试。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.