java字符串类型的日期处理问题
2008-04-18 11:32
671 查看
import java.text.*;
import java.util.*;
public class Test...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) throws Exception...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str1= new String("1986-02-4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str2= new String("1986-2-8");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
java.text.SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date d1 = simpleDateFormat.parse(str1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date d2 = simpleDateFormat.parse(str2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long dmm = d2.getTime()-d1.getTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int d =(int) dmm/1000/60/60/24;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println (d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str3="20070101000000";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SimpleDateFormat f2 = new SimpleDateFormat("yyyyMMddHHmmss");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date d3 = f2.parse(str3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println (d3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
字符串变成 java.sql.Date类型 .
如果 字符传满足 yyyy-MM-dd类型
java.sql.Date time = java.sql.Date.valueOf("2008-2-3");
自己把字符传变成 java.sql.Date类型 java.sql.Date是 java.util.Date 的子类.
import java.util.*;
public class Test...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) throws Exception...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str1= new String("1986-02-4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str2= new String("1986-2-8");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
java.text.SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date d1 = simpleDateFormat.parse(str1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date d2 = simpleDateFormat.parse(str2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long dmm = d2.getTime()-d1.getTime();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int d =(int) dmm/1000/60/60/24;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println (d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str3="20070101000000";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SimpleDateFormat f2 = new SimpleDateFormat("yyyyMMddHHmmss");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date d3 = f2.parse(str3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println (d3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
字符串变成 java.sql.Date类型 .
如果 字符传满足 yyyy-MM-dd类型
java.sql.Date time = java.sql.Date.valueOf("2008-2-3");
自己把字符传变成 java.sql.Date类型 java.sql.Date是 java.util.Date 的子类.
相关文章推荐
- java字符串类型的日期处理问题
- java字符串类型的日期处理问题
- java 字符串和日期类型的相互转换问题
- springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
- Java 日期格式是“Fri Feb 26 16:33:08 CST 2016”字符串类型,转date类型。java.util.date与sql.date的相互转换
- java判断字符串是否是日期类型
- 解决axis2处理java.util.Date类型对象时丢弃时间部分的问题
- TopCoder SRMS 1 字符串处理问题 Java题解
- SpringMVC日期格式类型转换问题--3种处理方法
- SpringMVC日期类型转换问题三大处理方法归纳
- [java]java字符串如何保存到数据库日期类型的列?
- SpringMVC日期类型转换问题处理方法归纳
- SpringMVC日期类型转换问题三大处理方法归纳
- IOS开发笔记-字符串转换为日期时间存在时差问题的处理
- 解决mysql 数据库中日期类型00:00:00 的问题 设置xml数据类型:java.util.Date
- 在java中实现日期类型和字符串类型的转换大全(Date String Timestamp Datetime)
- Flex+Java组合处理某些日期(如公元1111年)的问题
- java与C#字符串转换成日期处理比较
- SpringMVC日期类型转换问题三大处理方法归纳
- 29、Java中如何将字符串转换为日期类型?