//***************************************************
//名称:strToDate
//功能:将指定的字符串转换成日期
//输入:aStrValue: 要转换的字符串;
// aFmtDate: 转换日期的格式, 默认为:"yyyy/MM/dd"
// aDteRtn: 转换后的日期
//输出:
//返回:TRUE: 是正确的日期格式; FALSE: 是错误的日期格式
//***************************************************
public static boolean strToDate(
String aStrValue,
String aFmtDate,
java.util.Date aDteRtn)
{
if (aFmtDate.length() == 0)
{
aFmtDate = "yyyy/MM/dd";
}
SimpleDateFormat fmtDate = new SimpleDateFormat(aFmtDate);
try
{
aDteRtn.setTime(fmtDate.parse(aStrValue).getTime());
}
catch (Exception e)
{
return (false);
} return (true);
} //***************************************************
//名称:dateToStr
//功能:将指定的日期转换成字符串
//输入:aDteValue: 要转换的日期;
// aFmtDate: 转换日期的格式, 默认为:"yyyy/MM/dd"
//输出:
//返回:转换之后的字符串
//***************************************************
public static String dateToStr(java.util.Date aDteValue, String aFmtDate)
{
String strRtn = null; if (aFmtDate.length() == 0)
{
aFmtDate = "yyyy/MM/dd";
}
Format fmtDate = new SimpleDateFormat(aFmtDate);
try
{
strRtn = fmtDate.format(aDteValue);
}
catch (Exception e)
{ } return (strRtn);
}
//名称:strToDate
//功能:将指定的字符串转换成日期
//输入:aStrValue: 要转换的字符串;
// aFmtDate: 转换日期的格式, 默认为:"yyyy/MM/dd"
// aDteRtn: 转换后的日期
//输出:
//返回:TRUE: 是正确的日期格式; FALSE: 是错误的日期格式
//***************************************************
public static boolean strToDate(
String aStrValue,
String aFmtDate,
java.util.Date aDteRtn)
{
if (aFmtDate.length() == 0)
{
aFmtDate = "yyyy/MM/dd";
}
SimpleDateFormat fmtDate = new SimpleDateFormat(aFmtDate);
try
{
aDteRtn.setTime(fmtDate.parse(aStrValue).getTime());
}
catch (Exception e)
{
return (false);
} return (true);
} //***************************************************
//名称:dateToStr
//功能:将指定的日期转换成字符串
//输入:aDteValue: 要转换的日期;
// aFmtDate: 转换日期的格式, 默认为:"yyyy/MM/dd"
//输出:
//返回:转换之后的字符串
//***************************************************
public static String dateToStr(java.util.Date aDteValue, String aFmtDate)
{
String strRtn = null; if (aFmtDate.length() == 0)
{
aFmtDate = "yyyy/MM/dd";
}
Format fmtDate = new SimpleDateFormat(aFmtDate);
try
{
strRtn = fmtDate.format(aDteValue);
}
catch (Exception e)
{ } return (strRtn);
}
解决方案 »
- 关于GUI问题
- java方法重写问题
- 一道国外企业面试题,求助
- 【求助】请问添加到什么容器中的类才必须的同时重写equals方法和hashCode方法? 为什么添加到TreeSet中的不需要重写equals方法 和 hashCode 方法?
- 解压zip文件出错!java.lang.IllegalArgumentException
- java面试牛题!请高手解决
- 如何在单元级别控制绘制器,编辑器?
- 我想在上海考“软件工程硕士”,谁能告诉一些情况吗??????
- 关于HTMLConverter的问题
- 如何实现日期的加减?。。。。。。。。。。。。。。
- *************java菜鸟问个弱智问题.*************
- [求教]如何将两个工具栏并列地、竖直地停靠在Container的WEST一侧?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(a));
打印出来看看
import java.text.SimpleDateFormat;
class test
{
public static void main(String[] args)
{
long nCurrentTime = System.currentTimeMillis();
java.util.Date utilDate = new java.util.Date(nCurrentTime); GregorianCalendar da = new GregorianCalendar(2004, 11, 23, 11, 45, 50);
java.util.Date time = da.getTime();
java.sql.Date sqlDate = new java.sql.Date(time.getTime());
java.sql.Time sqlTime = new java.sql.Time(time.getTime());
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(nCurrentTime);
System.out.println(time);
System.out.println("sqlDate:-----"+sqlDate);
System.out.println("sqlTime:-----"+sqlTime);
System.out.println("sqlTimestamp:-----"+sqlTimestamp);
}
}
Date time = da.getTime();
java.sql.Date a = new java.sql.Date(da.getTimeInMillis());
System.out.println(time);
System.out.println(a);//转换前的格式SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(a));//转换后的格式
//把数据插入数据库,如果还没有小时、分、秒
//那么肯定是数据库字段格式设置问题