Calendar问题,50分,希望高手们多多探讨 Date now=new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");System.out.println(formatter.format(now)); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 to tomtian(老猫):这种方法我知道,但是我主要想搞清Calendar //********************************************* //功 能: //参 数: //返回值: //********************************************* public static String getTime() { String strTime = ""; cal = new GregorianCalendar(); nYear = cal.get(Calendar.YEAR); nMonth = cal.get(Calendar.MONTH); nDate = cal.get(Calendar.DAY_OF_MONTH); nHour = cal.get(Calendar.HOUR); nMin = cal.get(Calendar.MINUTE); nSec = cal.get(Calendar.SECOND); strTime += nYear; if (nMonth < 10) { strTime += "-0"; } else { strTime += "-"; } nMonth++; strTime += nMonth; if (nDate < 10) { strTime += "-0"; } else { strTime += "-"; } strTime += nDate; if (nHour < 10) { strTime += " 0"; } else { strTime += " "; } strTime += nHour; if (nMin < 10) { strTime += ":0"; } else { strTime += ":"; } strTime += nMin; if (nSec < 10) { strTime += ":0"; } else { strTime += ":"; } strTime += nSec; return strTime; } private static Calendar cal = null; private static int nYear = 0; private static int nMonth = 0; private static int nDate = 0; private static int nHour = 0; private static int nMin = 0; private static int nSec = 0; System.out.println(formatter.format(cal.toTime())); 你可以直接看到它的源码,Calendar是个抽象类,YEAR是一个final的变量默认的getInstance调用 new GeorageCalendar() System.out.pritnln(Calendar.YEAR+"-"+Calendar.MONTH+"-"+Calendar.DATE);老兄,Calendar.YEAR 只是一个常量 ,它永远都是1 .同样Calendar.MONTH 代表的是2,Calendar.DATE代表的是5 .它们是一个常量就像你自己定义一个 int ABCD = 5 一样这里的ABCD 就是 Calendar.YEAR .它只是一个名字,不是 Calendar.getYear() !!!!! 同意楼上的意见,但有个小小的问题Calendar类有getYear()方法马??这样就行了Calendar calendar = Calendar.getInstance();System.out.print(calendar.get(Calendar.MONTH)+1); 菜鸟问题 请高手解答 如何改变RCP程序的外观-比如颜色 如何将 Sting a = "TEST2008" 生成二进制文件 请教java税率输入输出问题? 有关dtree刷新的问题 java面试题,纠结了半天只做了一半,在线等高手解答! 问各位一个关于applet的问题!(散分50) 请教如何加入main函数,能执行这个java小程序(附源代码) 急!高分求助:水晶报表在Web上的打印问题 大家给点我继续学习JAVA的学习,我希望大家能改变我的观点。 如何从string中提取自己需要的一部分字符 请帮我看看这个变量(属性)属于什么类型,谢谢!
//功 能:
//参 数:
//返回值:
//*********************************************
public static String getTime()
{
String strTime = "";
cal = new GregorianCalendar();
nYear = cal.get(Calendar.YEAR);
nMonth = cal.get(Calendar.MONTH);
nDate = cal.get(Calendar.DAY_OF_MONTH);
nHour = cal.get(Calendar.HOUR);
nMin = cal.get(Calendar.MINUTE);
nSec = cal.get(Calendar.SECOND); strTime += nYear;
if (nMonth < 10)
{
strTime += "-0";
}
else
{
strTime += "-";
}
nMonth++;
strTime += nMonth; if (nDate < 10)
{
strTime += "-0";
}
else
{
strTime += "-";
}
strTime += nDate; if (nHour < 10)
{
strTime += " 0";
}
else
{
strTime += " ";
}
strTime += nHour; if (nMin < 10)
{
strTime += ":0";
}
else
{
strTime += ":";
}
strTime += nMin; if (nSec < 10)
{
strTime += ":0";
}
else
{
strTime += ":";
}
strTime += nSec;
return strTime; }
private static Calendar cal = null; private static int nYear = 0;
private static int nMonth = 0;
private static int nDate = 0;
private static int nHour = 0;
private static int nMin = 0;
private static int nSec = 0;
默认的getInstance调用 new GeorageCalendar()
Calendar.DATE代表的是5 .它们是一个常量就像你自己定义一个 int ABCD = 5 一样
这里的ABCD 就是 Calendar.YEAR .它只是一个名字,不是 Calendar.getYear() !!!!!
这样就行了
Calendar calendar = Calendar.getInstance();
System.out.print(calendar.get(Calendar.MONTH)+1);