当前位置: 技术问答>java相关
这个类写的有问题吗?初学问题
来源: 互联网 发布时间:2015-06-21
本文导语: import java.util.Date; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; public class DateTimeConv { protected String sDateTime; protected long lDateTime; protected boolean bConver = false; protected Date dDate = new Da...
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class DateTimeConv
{
protected String sDateTime;
protected long lDateTime;
protected boolean bConver = false;
protected Date dDate = new Date();
/**
* Constructor for DateTimeConv.
*/
public DateTimeConv(String sDateTime)
{
SetTime(sDateTime);
}
public DateTimeConv(long lDateTime)
{
SetTime(lDateTime);
}
protected void Conver()
{
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
dDate = sf.parse(sDateTime);
}
catch(ParseException e)
{
e.printStackTrace();
return;
}
lDateTime = dDate.getTime() ;
bConver = true;
}
public int GetIntVal()
{
return (int)lDateTime;
}
public long GetLongVal()
{
return lDateTime;
}
public String GetYear()
{
return Format("yyyy");
}
public String GetMonth()
{
return Format("MM");
}
public String GetDay()
{
return Format("dd");
}
public String GetHour()
{
return Format("mm");
}
public String GetSecond()
{
return Format("ss");
}
public String GetMinute()
{
return Format("MM");
}
protected String Format(String sFormat)
{
if(!bConver)
return null;
String sVal;
SimpleDateFormat sf = new SimpleDateFormat(sFormat);
sVal = sf.format(dDate);
return sVal;
}
public void SetTime(String sDateTime)
{
this.sDateTime = sDateTime;
Conver();
}
public void SetTime(long lDateTime)
{
this.lDateTime = lDateTime;
dDate.setTime(lDateTime);
bConver = true;
}
public static void main(String[] args)
{
DateTimeConv dc = new DateTimeConv("1994-03-04 21:51:03");
String s;
s = dc.GetYear() + "-" + dc.GetMonth() + "-" + dc.GetMonth() + " "
+ dc.GetHour() + ":" + dc.GetMinute() + ":" + dc.GetSecond();
System.out.println(s);
System.out.println(dc.GetLongVal());
long l = 76797306;
dc = new DateTimeConv(l);
s = dc.GetYear() + "-" + dc.GetMonth() + "-" + dc.GetMonth() + " "
+ dc.GetHour() + ":" + dc.GetMinute() + ":" + dc.GetSecond();
System.out.println(s);
System.out.println(dc.GetLongVal());
}
}
|
可能错误出在这