英文:
JAVA -Conversion of one date format to another. Unhandled exception type ParseException
问题
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("02-JAN-2020");
list.add("06-APR-2020");
list.add("09-ARR-2020");
list.add("07-MAY-2020");
int j;
String array[] = new String[list.size()];
for (j = 0; j < list.size(); j++) {
array[j] = list.get(j);
}
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
for (int counter = 0; counter < j; counter++) {
Date date = inputFormat.parse(array[counter]);
String outputText = outputFormat.format(date);
System.out.println(outputText);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
英文:
Please find the below program to convert dates from one format to another.
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("02-JAN-2020");//Adding object in arraylist
list.add("06-APR-2020");
list.add("09-ARR-2020");
list.add("07-MAY-2020");
int j;
//converted array list into string array
String array[] = new String[list.size()];
for( j =0;j<list.size();j++){
array[j] = list.get(j);
}
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("dd-MMM-yyyy");
//convert the input format to the output format
for (int counter = 0; counter < j; counter++) {
Date date = inputFormat.parse(array0+网站访问量);
String outputText = outputFormat.format(date);
System.out.println(outputText);
}
}
}
While running the program, I'm getting this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type ParseException
答案1
得分: 0
你需要使用 throw 或者使用 try/catch 来处理 ParseException。
public static void main(String[] args) throws ParseException {
}
英文:
You need to throw or use try/catch for parseException
public static void main(String[] args) throws ParseException {
}
专注分享java语言的经验与见解,让所有开发者获益!
评论