将日历转换为字符串

时间:2016-03-04 06:51:06

标签: java string date calendar

我想将Calendar输出转换为String。以下是我的代码

Calendar cal=Calendar.getInstance();
System.out.println("Time is "+cal.getTime());
String str=cal.getTime();   //This is how I want to store the output, but this is wrong.

这里getTime()方法的输出是'Date'类型,所以得到错误。 有没有办法进行类型转换或任何解决方案?

1 个答案:

答案 0 :(得分:1)

请参阅:

Calendar cal=Calendar.getInstance(); 
System.out.println("Time is "+cal.getTime()); 
String str=cal.toString();
System.out.println("Time is "+str);