JAVA -Conversion of one date format to another. Unhandled exception type ParseException

huangapple 未分类评论42阅读模式
英文:

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&lt;String&gt; list=new ArrayList&lt;String&gt;();//Creating arraylist    
	      list.add(&quot;02-JAN-2020&quot;);//Adding object in arraylist    
	      list.add(&quot;06-APR-2020&quot;);    
	      list.add(&quot;09-ARR-2020&quot;);    
	      list.add(&quot;07-MAY-2020&quot;);    
	      
	      int j;
	       
	      //converted array list into string array
	      String array[] = new String[list.size()];              
			for( j =0;j&lt;list.size();j++){
			  array[j] = list.get(j);
			}
		 
		  DateFormat outputFormat = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);
		  DateFormat inputFormat = new SimpleDateFormat(&quot;dd-MMM-yyyy&quot;);
		  
		 //convert the input format to the output format
		  for (int counter = 0; counter &lt; j; counter++) { 	
			  		  
		  Date date = inputFormat.parse(array
0
+
网站访问量
); 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 {


 }

huangapple
  • 本文由 发表于 2020年7月24日 18:01:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/63071325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定