package test;
import java.util.Date;
public class mytime
{
String now_dd = ""; //当前日期
String str_dd = "";
long long_dd = 0;
public String NowDate(){
java.util.Date dtnowday = new java.util.Date();
int iydtnowday = dtnowday.getYear()+1900;
int imdtnowday = dtnowday.getMonth()+1;
now_dd= iydtnowday+"/"+imdtnowday+"/"+dtnowday.getDate();
return now_dd; //返回当前日期String
}
public long getLongTime(String dd){
java.text.SimpleDateFormat temp = new java.text.SimpleDateFormat("yyyy/MM/dd",java.util.Locale.US);
java.util.Date todayTime = temp.parse(dd);
long_dd = todayTime.getTime();
return long_dd; //返回被转换了的String型日期(String --> Long)
}
public String getStrTime(long dd){
java.text.SimpleDateFormat temp1 = new java.text.SimpleDateFormat("yyyy/MM/dd",java.util.Locale.US);
java.util.Date e1=new java.util.Date(dd);
str_dd = temp1.format(e1);
return str_dd; //返回被转换了的long型日期(Long --> String)
}
}谢谢各位!
import java.util.Date;
public class mytime
{
String now_dd = ""; //当前日期
String str_dd = "";
long long_dd = 0;
public String NowDate(){
java.util.Date dtnowday = new java.util.Date();
int iydtnowday = dtnowday.getYear()+1900;
int imdtnowday = dtnowday.getMonth()+1;
now_dd= iydtnowday+"/"+imdtnowday+"/"+dtnowday.getDate();
return now_dd; //返回当前日期String
}
public long getLongTime(String dd){
java.text.SimpleDateFormat temp = new java.text.SimpleDateFormat("yyyy/MM/dd",java.util.Locale.US);
java.util.Date todayTime = temp.parse(dd);
long_dd = todayTime.getTime();
return long_dd; //返回被转换了的String型日期(String --> Long)
}
public String getStrTime(long dd){
java.text.SimpleDateFormat temp1 = new java.text.SimpleDateFormat("yyyy/MM/dd",java.util.Locale.US);
java.util.Date e1=new java.util.Date(dd);
str_dd = temp1.format(e1);
return str_dd; //返回被转换了的long型日期(Long --> String)
}
}谢谢各位!
变量小写开头,后面的单词以大写接上不以下划线
get、set都要跟变量对应,不然肯定出问题
比如String nowDD,那么赋值定义为setNowDD(..),取值定义为getNowDD()
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = dateFormat.parse(dd) ;
long_dd = d.getTime();
return long_dd ;
}太仓促,没调试,多包涵
unreported exception java.text.ParseException;must be caught or declared to be thrown
java.util.Date todayTime = temp.parse(dd);
Note;mytime.java uses or overrides a deprecated API.
Note:Recompile with -deprecation for details.打得好累。。为什么DOS窗口不能复制啊。。谢谢楼上二位,可是还是没解决。。难道JAVABEAN语法出错了吗?
选择内容已在粘贴区里 Ctrl+V即可
try {
todayTime = temp.parse(dd);
}
catch (ParseException ex) {
}
这样应该没问题了
555555~~~
还有,为什么一定要加捕获?不加为什么会出错!
Date类的getYear等方法都deprecated,不建议使用,用Calendar类吧。
parse
public Number parse(String source)
throws ParseException//使用该方法时可能抛出异常
Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.
See the parse(String, ParsePosition) method for more information on number parsing.
Parameters:
source - A String whose beginning should be parsed.
Returns:
A Number parsed from the string.
Throws:
ParseException - if the beginning of the specified string cannot be parsed