sqlserver MS是自动转的,直接string就转成(数据库的)date了,直接就可以between and
import java.util.*; import java.text.SimpleDateFormat;public class TestSimpleDateForma { public static void main(String[] args) { Calendar cal =Calendar.getInstance(); cal.set(2009,5,1);
//需要转换的格式 SimpleDateFormat now = new SimpleDateFormat("yyyy年MM月dd日"); //打印结果 System.out.println(now.format(cal.getTime())); //2009年06月05日,注意:cal.set(2009,5,1); } }
String sd=request.getParameter("sdate"); String ed=request.getParameter("edate"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date sdate = new Date(); Date edate = new Date(); try { sdate = sdf.parse(sd); edate = sdf.parse(ed); }catch (java.text.ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(sdate); System.out.println(edate);这是我以前写的,可是输入来的不是我想 要的格式呀
import java.text.SimpleDateFormat; import java.util.Date; public class DateFormat { public static void main(String[] args) { // TODO Auto-generated method stub Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日"); String formatDate= sdf.format(date); System.out.println("时间格式:"+formatDate); //时间格式:2009年-08月-27日 }}请参考
那我要怎么做才能达到我的目的 select * from table where fdate between 起始日期 and 终止日期
String sd=request.getParameter("sdate"); String ed=request.getParameter("edate"); 那我直接 select * from table where fdate between sd and ed ????
这样做完全可以 id1为你接收的起始时间 id2为你的接收的结止时间String sql="select * from table where date between '" + id1 + "' and '" + id2 + "' order by date"
假如后面还要跟条件 and 就好!!
在SQL SERVER里这样写没问题在Oracle里需要这样转换一下 BETWEEN TO_DATE('2009-08-01', 'YYYY-MM-DD') AND TO_DATE('2009-08-10', 'YYYY-MM-DD')
可是他表里面的fdate是datetime类型的 我这样写之后查不出数据,都是空的
那你里写错了吗? 我的数据库的也是个数据类型datetime 还测试了
String sql="select * from table where fdate between '" + fd + "' and '" + ed + "' order by date"String sd=request.getParameter("sdate").trim(); String ed=request.getParameter("edate").trim();
DateFormat中的方法比较: format(Date date)将一个 Date 格式化为日期/时间字符串; parse(String source)从给定字符串的开始解析文本,以生成一个日期。 在这你可以使用format方法格式化成你自己想要的格式。
String a=dateformat.format(new Date());
System.out.println(a);
String a=dateformat.format(new Date());
System.out.println(a);
import java.util.*;
import java.text.SimpleDateFormat;public class TestSimpleDateForma {
public static void main(String[] args) { Calendar cal =Calendar.getInstance();
cal.set(2009,5,1);
//需要转换的格式
SimpleDateFormat now = new SimpleDateFormat("yyyy年MM月dd日");
//打印结果
System.out.println(now.format(cal.getTime()));
//2009年06月05日,注意:cal.set(2009,5,1);
}
}
String ed=request.getParameter("edate");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date sdate = new Date();
Date edate = new Date();
try {
sdate = sdf.parse(sd);
edate = sdf.parse(ed);
}catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sdate);
System.out.println(edate);这是我以前写的,可是输入来的不是我想 要的格式呀
import java.util.Date;
public class DateFormat { public static void main(String[] args) {
// TODO Auto-generated method stub
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日");
String formatDate= sdf.format(date);
System.out.println("时间格式:"+formatDate);
//时间格式:2009年-08月-27日
}}请参考
一个日期类型不会print出为yyyy-mm-dd这种格式
select * from table where fdate between 起始日期 and 终止日期
String ed=request.getParameter("edate");
那我直接
select * from table where fdate between sd and ed ????
id1为你接收的起始时间
id2为你的接收的结止时间String sql="select * from table where date between '" + id1 + "' and '" + id2 + "' order by date"
在SQL SERVER里这样写没问题在Oracle里需要这样转换一下
BETWEEN TO_DATE('2009-08-01', 'YYYY-MM-DD') AND TO_DATE('2009-08-10', 'YYYY-MM-DD')
我这样写之后查不出数据,都是空的
我的数据库的也是个数据类型datetime
还测试了
String sql="select * from table where fdate between '" + fd + "' and '" + ed + "' order by date"String sd=request.getParameter("sdate").trim();
String ed=request.getParameter("edate").trim();
format(Date date)将一个 Date 格式化为日期/时间字符串;
parse(String source)从给定字符串的开始解析文本,以生成一个日期。
在这你可以使用format方法格式化成你自己想要的格式。