如何将string类型转换成date类型?急

qfqufeng 2002-05-10 04:45:09
例如:
String s="1998-01-02";
Date d;
d=?

谢谢,急!!
...全文
582 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcgui 2002-05-11
  • 打赏
  • 举报
回复
up
caomeijyhap 2002-05-10
  • 打赏
  • 举报
回复
ok,好了,
非常谢谢两位的帮忙.^_^
wangwenyou 2002-05-10
  • 打赏
  • 举报
回复
import java.text.SimpleDateFormat
qfqufeng 2002-05-10
  • 打赏
  • 举报
回复
第二种方法我用了,还是出问题:

class simpledateformat not found in class add_0002ejsp_jsp at line46
qfqufeng 2002-05-10
  • 打赏
  • 举报
回复
第二种方法我用了,还是出问题:

class simpledateformat not found in class add_0002ejsp_jsp at line46
qfqufeng 2002-05-10
  • 打赏
  • 举报
回复
我的程序如下:
<%@ page language="java" import="java.sql.*,java.text.DateFormat " %>

Date rxsj1;
DateFormat dateFormat;
dateFormat = new SimpleDateFormat( "yyyy-mm-dd",Locale.ENGLISH );
dateFormat.setLenient( false );
rxsj1=dateFormat.parse( rxsj );

直接取值不知行不行?
可以的话,有问题
1.class simpledateformat not found in class add_0002ejsp_jsp at line46
2.variable locale not found in class add_0002ejsp_jsp at line46
3.incompatible types:found:java.util.date,required:java.sql.date at line 48

请问如何操作呢?
locale 是什么呢?
cloudjava 2002-05-10
  • 打赏
  • 举报
回复
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
String s="1998-01-02";
Date date = formatter.parse(s);
wangwenyou 2002-05-10
  • 打赏
  • 举报
回复
/**
* 将Date型数据的字符表示串。
* @param dateString Date数据的字符串表示
* @return 解析得到的Date型数据
*/
public final static String date2String( Date date )
throws java.lang.Exception
{
DateFormat dateFormat;
dateFormat = new SimpleDateFormat( "yyyy-MM-dd kk:mm:ss",
Locale.ENGLISH );
dateFormat.setLenient( false );

return dateFormat.format( date );
}
wangwenyou 2002-05-10
  • 打赏
  • 举报
回复
/**
* 将Date型数据的字符表示串。
* @param dateString Date数据的字符串表示
* @return 解析得到的Date型数据
*/
public final static String date2String( Date date )
throws java.lang.Exception
{
DateFormat dateFormat;
dateFormat = new SimpleDateFormat( "yyyy-MM-dd kk:mm:ss",
Locale.ENGLISH );
dateFormat.setLenient( false );

return dateFormat.format( date );
}
wangwenyou 2002-05-10
  • 打赏
  • 举报
回复
你参照ApiDoc改一下SimpleDateFormat构造参数就行了:)
/**
* 将字符串解析为Date型数据。
* @param dateString Date数据的字符串表示
* @return 解析得到的Date型数据
*/
public final static Date string2Date( String dateString )
throws java.lang.Exception
{
DateFormat dateFormat;
dateFormat = new SimpleDateFormat( "EEE MMM dd kk:mm:ss z yyyy",
Locale.ENGLISH );
dateFormat.setLenient( false );

return dateFormat.parse( dateString );
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧