分类: Java
2010-09-15 13:50:33
Java 中对日期的操作
Java 中对日期的操作........................................................................................................ 1
1 日期模板...................................................................................................................... 2
2 格式化日期................................................................................................................... 2
3 取得当前日期............................................................................................................... 3
4 当前时间...................................................................................................................... 3
5 1 年前日期.................................................................................................................... 4
6 明天日期...................................................................................................................... 4
7 两个时间之间的天数..................................................................................................... 4
8 加半小时...................................................................................................................... 4
9 年月周求日期............................................................................................................... 5
10 求是星期几................................................................................................................. 5
何海平hp54@163.com
11 java 数据类型转换........................................................................................................ 5
11.1 如何将字串String 转换成整数int....................................................................... 5
11.2 如何将整数int 转换成字串String ..................................................................... 5
11.3 例子.................................................................................................................. 6
11.4 数据类型转换函数............................................................................................. 7
12 取得序号加一............................................................................................................. 8
1 日期模板
模板定义如下:
y 年号,如1996
M 月份,如July 或者07
d 月中第几天,如12
H 小时(24 制),如0、17
m 分钟,如32
s 钞钟,如55
S 微钞,如978
E 星期几,如Tuesday
D 一年中的第几天,如189
w week in year (Number) 27
W week in month (Number) 2
a am/pm marker (Text) PM
k hour in day (1~24) (Number) 24
K hour in am/pm (0~11) (Number) 0
z time zone (Text) Pacific Standard Time
' escape for text (Delimiter)
'' single quote (Literal) '
2 格式化日期
使用SimpleDateFormat 类格式化日期,如果日期格式不对,会产生异常。
import java.text.*;
import java.util.*;
public class TestDate {
public static void main(String[] args) {
String dStr = "2001.12.12-08.23.21";
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd- HH.mm.ss");
try {
何海平hp54@163.com
d = sdf.parse(dStr);
} catch (ParseException pe) {
System.out.println(pe.getMessage());
}
System.out.println(d);
System.out.println(d.getTime());
}
}
在java.text 包中找到那个SimpleDateFormat 类,给它一个模板,可以parse 成一个Date,然后使用Date
类的getItme() 方法,可以得到一个Long 数据。
3 取得当前日期
public String GetDateTime()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy- MM-dd HH:mm:ss");
String mDateTime=formatter.format(cal.getTime());
return (mDateTime);
}
public java.sql.Date GetDate()
{
java.sql.Date mDate;
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy- MM-dd HH:mm:ss");
String mDateTime=formatter.format(cal.getTime());
return (java.sql.Date.valueOf(mDateTime));
}
4 当前时间
Calendar cal = Calendar.getInstance();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM- dd HH:mm:ss");
何海平hp54@163.com
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");
String mDateTime=formatter.format(cal.getTime());
out.println(mDateTime);
out.println("
");
5 1 年前日期
java.util.Date myDate=new java.util.Date();
long myTime=(myDate.getTime()/1000)-60*60*24*365;
myDate.setTime(myTime*1000);
String mDate=formatter.format(myDate);
out.println(mDate);
out.println("
");
6 明天日期
myDate=new java.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
out.println(mDate);
out.println("
");
7 两个时间之间的天数
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM- dd");
java.util.Date date= myFormatter.parse("2003-05-1");
java.util.Date mydate= myFormatter.parse("1899-12-30");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
out.println(day);
out.println("
");
8 加半小时
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date date1 = format.parse("2002-02-28 23:16:00");
long Time=(date1.getTime()/1000)+60*30;
date1.setTime(Time*1000);
何海平hp54@163.com
String mydate1=formatter.format(date1);
out.println(mydate1);
out.println("
");
9 年月周求日期
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
java.util.Date date2= formatter2.parse("2003-05 5 星期五");
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
out.println(mydate2);
out.println("
");
10 求是星期几
mydate= myFormatter.parse("2001-1-1");
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
String mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("
");
11 java 数据类型转换
11.1 如何将字串String 转换成整数int
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成Double, Float, Long 的方法大同小异.
11.2 如何将整数int 转换成字串String
A. 有叁种方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
何海平hp54@163.com
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异.
11.3 例子
package cn.com.lwkj.erts.register;
import java.sql.Date;
public class TypeChange {
public TypeChange() {
}
//change the string type to the int type
public static int stringToInt(String intstr)
{
Integer integer;
integer = Integer.valueOf(intstr);
return integer.intValue();
}
//change int type to the string type
public static String intToString(int value)
{
Integer integer = new Integer(value);
return integer.toString();
}
//change the string type to the float type
public static float stringToFloat(String floatstr)
{
Float floatee;
floatee = Float.valueOf(floatstr);
return floatee.floatValue();
}
//change the float type to the string type
public static String floatToString(float value)
何海平hp54@163.com
{
Float floatee = new Float(value);
return floatee.toString();
}
//change the string type to the sqlDate type
public static java.sql.Date stringToDate(String dateStr)
{
return java.sql.Date.valueOf(dateStr);
}
//change the sqlDate type to the string type
public static String dateToString(java.sql.Date datee)
{
return datee.toString();
}
public static void main(String[] args)
{
java.sql.Date day ;
day = TypeChange.stringToDate("2003-11-3");
String strday = TypeChange.dateToString(day);
System.out.println(strday);
}
}
11.4 数据类型转换函数
JAVA 中常用数据类型转换函数
虽然都能在JAVA API 中找到,整理一下做个备份。
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
何海平hp54@163.com
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double)
12 取得序号加一
public int GetMaxID(String vTableName,String vFieldName)
{
int mResult=0;
boolean mConn=true;
String mSql=new String();
mSql = "select max("+vFieldName+")+1 as MaxID from " + vTableName;
try
{
何海平hp54@163.com
if (Conn!=null){
mConn=Conn.isClosed();
}
if (mConn){
OpenConnection();
}
ResultSet result=ExecuteQuery(mSql);
if (result.next())
{
mResult=result.getInt("MaxID");
}
result.close();
if (mConn)
{
CloseConnection();
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
return (mResult);
}
mDocumentId=DbaObj.GetMaxID("Document","DocumentID");