java 时间的类型_Java 时间类型详解

标签:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

1.Calendar转化String

//获取当前时间的具体情况,如年,月,日,week,date,分,秒等

Calendar calendat =

Calendar.getInstance();

SimpleDateFormat

sdf = new SimpleDateFormat("yyyy-MM-dd");

String dateStr = sdf.format(calendar.getTime());

2.String转化Calendar

String

str="2010-5-27";

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

Date

date =sdf.parse(str);

Calendar

calendar = Calendar.getInstance();

calendar.setTime(date);

3.Date转化String

SimpleDateFormat

sdf= new SimpleDateFormat("yyyy-MM-dd");

String

dateStr=sdf.format(new Date());

4.String转化Date

String str="2010-5-27";

SimpleDateFormat

sdf= new SimpleDateFormat("yyyy-MM-dd");

Date

birthday = sdf.parse(str);

5.Date转化Calendar

Calendar

calendar = Calendar.getInstance();

calendar.setTime(new java.util.Date());

6.Calendar转化Date

Calendar

calendar = Calendar.getInstance();

java.util.Date date =calendar.getTime();

7.Date转成String

System.out.println(sdf.format(new

Date()));

8.String转成Timestamp

Timestamp

ts = Timestamp.valueOf("2011-1-14 08:11:00");

9.Timestamp转成String

sdf.format(ts);

Timestamp和Date多数用法是一样的。

10.Date转TimeStamp

SimpleDateFormat

df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String

time = df.format(new Date());

Timestamp

ts = Timestamp.valueOf(time);

11.日期比较大小

String

ti="2010-11-25 20:11:00";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd

HH:mm:ss");

Date time=sdf.parse(ti);

String

ti2="2011-11-26 20:11:00";

Date time2=sdf.parse(ti2);

int

c=ti2.compareTo(ti);

if(c>0){

System.out.println(ti+"大");

}else if(c==0){

System.out.println("一样大");

}else{

System.out.println(ti2+"大");

}

12.Date/

Timestamp转成Calendar

Calendar

calendar = Calendar.getInstance();

calendar.setTime(startDate);

calendar.add(Calendar.YEAR,

2);   //日期加2年

System.out.println(calendar.getTime());

calendar.add(Calendar.DATE, -30);     //日期加30天

System.out.println(calendar.getTime());

calendar.add(Calendar.MONTH, 3);  //日期加3个月

System.out.println(calendar.getTime());

DateUtil

/**

* 时间格式的转换(Timestamp转换为string)

* @author zcm

* @param time

* @return String类型的时间格式字符串

*/

public static String

convert(Timestamp time) {

SimpleDateFormat df = new

SimpleDateFormat("yyyy-MM-dd

HH:mm:ss");

String result =

df.format((Date) time);

return

result;

}

/**

* 获取当时系统时间(timestamp)

* @author zcm

* @return Timestamp类型的时间格式

*/

public static

Timestamp getNow() {

Date date = new

Date();

SimpleDateFormat simDate = new

SimpleDateFormat("yyyy-MM-dd

HH:mm:ss");

Timestamp now=Timestamp.valueOf(simDate.format(date));

return now;

}

/**

* 将date转换为string类型yyyy-MM-dd

* @param date

* @return

*@author Jimmy

*/

public static String

dateConvertToString(Date date){

SimpleDateFormat formatter

= new SimpleDateFormat( "yyyy-MM-dd ");

String time =

formatter.format(date);//格式化数据

return time;

}

/**

* 获取当前时间的毫秒数

* @return

*@author Jimmy

*/

public static

Long  getTime(){

Date dt= new

Date();

Long time= dt.getTime();

return time;

}

/**

* 将字符串类型转换为calendar

* @return

*@author Jimmy

* @throws ParseException

*/

public static

Calendar convertToCalendar(String date) throws

ParseException{

Date d = new

SimpleDateFormat("yyyy-MM-dd").parse(date);

Calendar cal=Calendar.getInstance();

cal.setTime(d);

return cal;

}

public static void

main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance();

System.out.println("年: " +

now.get(Calendar.YEAR));

System.out.println("月: " +

(now.get(Calendar.MONTH) + 1) + "");

System.out.println("日: " +

now.get(Calendar.DAY_OF_MONTH));

System.out.println("时: " +

now.get(Calendar.HOUR_OF_DAY));

System.out.println("分: " +

now.get(Calendar.MINUTE));

System.out.println("秒: " +

now.get(Calendar.SECOND));

System.out.println("当前时间毫秒数:" +

now.getTimeInMillis());

System.out.println("当前月的天数:" +

now.getActualMaximum(Calendar.DATE));

System.out.println(now.getTime());

//给定日期查看最大数

now.set(Calendar.YEAR,2002);

now.set(Calendar.MONTH,6);//7月

int   maxDate   =

now.getActualMaximum(Calendar.DATE);

Date d = new

Date();

System.out.println(d);

SimpleDateFormat sdf = new

SimpleDateFormat("yyyy-MM-dd

HH:mm:ss");

String dateNowStr = sdf.format(d);

System.out.println("格式化后的日期:" +

dateNowStr);

String str = "2012-1-13 17:26:33";  //要跟上面sdf定义的格式一样

Date today = sdf.parse(str);

System.out.println("字符串转成日期:" +

today);

}

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值