[英]Java: Extract values from a String?
I'm new to Java and am trying to extract values from a date-related user input string and split it into a DD-MM-YYYY format. 我是Java的新手,正在尝试从与日期相关的用户输入字符串中提取值,并将其拆分为DD-MM-YYYY格式。
I tried to use the code below: 我尝试使用下面的代码:
String inputString = "s.param1";
String[] arraySplit_3 = inputString.split("-");
for (int i=0; i < arraySplit_3.length; i++){
System.out.println(arraySplit_3[i]);
}
s.param1
gets the input of the user, I use a separate excel file for it. s.param1
获取用户的输入, s.param1
我使用了一个单独的excel文件。
if s.param1 = 15-05-2010 如果s.param1 = 15-05-2010
I wish to get this output: 我希望得到以下输出:
DD: 15 DD:15
MM: 05 MM:05
YYYY: 2010 YYYY:2010年
Is there a way to create a method like this? 有没有办法创建这样的方法?
Use java.time.LocalDate
to parse this. 使用java.time.LocalDate
进行解析。
// dd: Day of Month
// MM: Month of year
// yyyy: year, four digits
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(inputString);
System.out.printf("DD: %d%nMM: %d%nYYYY: %n",
date.getDayOfMonth(), date.getMonthValue(), date.getYear());
You are almost there, but maybe do not bother using a loop 您快到了,但也许不用打扰
String inputString = "15-05-2010";
String[] arraySplit_3 = inputString.split("-");
System.out.println("DD: " + arraySplit_3[0]);
System.out.println("MM: " + arraySplit_3[1]);
System.out.println("YYYY: " + arraySplit_3[2]);
But if you are wanting something more exotic look for SimpleDateFormat 但是,如果您想要更多奇特的东西,请查看SimpleDateFormat
Why you don't pars to date/calendar Object ? 为什么不解析日期/日历对象?
String input = "15-05-2010";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Date date = format.parse(input);
Calendar calendar = Calendar.getInstance() ;
calendar.setTime(date);
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
If your s.param1
is variable which gets date in String, so you shouldn't use it in quotes. 如果您的s.param1
是在String中获取日期的变量,那么您不应在引号中使用它。 Otherwise it will be String. 否则它将是String。 And variable name could not be with dot . 变量名不能带点号 。 It could be sParam1
. 可能是sParam1
。
But when you set date instead of s.param1
your created method should work. 但是,当您设置日期而不是s.param1
您创建的方法应该可以使用。
// input "15-05-2010"
String inputString = sparam1;
String[] arraySplit_3 = inputString.split("-");
for (int i=0; i < arraySplit_3.length; i++){
System.out.println(arraySplit_3[i]);
}
The output will be: 输出将是:
15 15
05 05
2010 2010
If you want to add some chars before the numbers don't use for
loop. 如果要在数字前添加一些字符,请不要使用for
循环。 Use it like this: 像这样使用它:
// ...
if (arraySplit_3 > 2) {
System.out.println("DD: " + arraySplit_3[0]);
System.out.println("MM: " + arraySplit_3[1]);
System.out.println("YYYY: " + arraySplit_3[2]);
}
Then output will be: 然后输出将是:
DD: 15 DD:15
MM: 05 MM:05
YYYY: 2010 YYYY:2010年
If you want to use a loop for that i would suggest You to create spearated method for splitting values here i have an example code 如果您想为此使用一个循环,我建议您在这里创建用于拆分值的带刺方法,我有一个示例代码
public class licz {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("15-05-2010");
list.add("18-02-2015");
extractValues(list);
}
private static void extractValues(List<String> list) {
int change = 0;
for (int i=0; i < list.size(); i++) {
String[] split = list.get(i).split("-");
for (String splitElement : split) {
switch (change) {
case 0:
System.out.println("DD: " + split[0]);
change++;
break;
case 1:
System.out.println("MM: " + split[1]);
change++;
break;
case 2:
System.out.println("YYYY: " + split[2]);
change=0;
break;
}
}
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.