java date类的使用_Java Date类的使用

有时候我们需要将一个字符串类型转换为一个日期类型,也需要将日期类型格式化为字符串类型,可以结合Date类和SimpleDateFormat一起使用,具体代码如下:

package com.fish.other;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/*

日期类 Date

Calendar

日期格式化类    SimpleDateFormat

*/

public class Demo3  {

public static void main(String[] args) throws ParseException {

/*

Date date = new Date(); // 获取当前的系统时间

System.out.println("年份:"+ date.getYear()); //Date类下面的很多方法都过时了

*/

Calendar calendar = Calendar.getInstance(); //获取当前的系统时间。

System.out.println("年:"+ calendar.get(Calendar.YEAR));

System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1));

System.out.println("日:"+ calendar.get(Calendar.DATE));

System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY)); //HOUR_OF_DAY 24小时制

System.out.println("分:"+ calendar.get(Calendar.MINUTE));

System.out.println("秒:"+ calendar.get(Calendar.SECOND));

/*

显示 当前系统时间: xxxx年xxx月xx日  xx时xx分xx秒

*  日期格式化类    SimpleDateFormat

*  作用1: 可以把日期转换转指定格式的字符串     format()

*  作用2: 可以把一个 字符转换成对应的日期。    parse()   生日

*

*/

Date date = new Date(); //获取当前的系统时间。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss") ; //使用了默认的格式创建了一个日期格式化对象。

String time = dateFormat.format(date);  //可以把日期转换转指定格式的字符串

System.out.println("当前的系统时间:"+ time);

String birthday = "2000年12月26日   11:29:08";

Date date2 = dateFormat.parse(birthday);  //注意: 指定的字符串格式必须要与SimpleDateFormat的模式要一致。

System.out.println(date2);

Date date21 =new Date();

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");

String time2 =dateFormat.format(date21);

String time21=dateFormat.format(date);

System.out.println("当前的系统时间:"+time);

System.out.println("当前的系统时间:"+time21);

String birthday1= "2000年12月26日   11:28:08";

Date date22=dateFormat.parse(birthday1);

System.out.println(date22);

}

}

/*

*

年:2016

月:3

日:15

时:20

分:52

秒:36

当前的系统时间:2016年03月15日   20:52:36

Tue Dec 26 11:29:08 CST 2000

当前的系统时间:2016年03月15日   20:52:36

当前的系统时间:2016年03月15日   20:52:36

Tue Dec 26 11:28:08 CST 2000

*

*/

二、Math类

package com.fish.other;

/*

Math 数学类, 主要是提供了很多的数学公式。

abs(double a)  获取绝对值

ceil(double a)  向上取整

floor(double a)  向下取整

round(float a)   四舍五入

random()   产生一个随机数. 大于等于 0.0 且小于 1.0 的伪随机 double 值

*/

public class Demo4 {

public static void main(String[] args) {

System.out.println("绝对值:"+Math.abs(-3));

System.out.println("向上取整:"+Math.ceil(3.14));

System.out.println("向下取整:"+Math.floor(-3.14)); //

System.out.println("四舍五入:"+Math.round(3.54));

System.out.println("随机数:"+Math.random());

}

}

/*

绝对值:3

向上取整:4.0

向下取整:-4.0

四舍五入:4

随机数:0.6917732509917355

*/

三、Random类的使用

package com.fish.other;

import java.util.Random;

/*

随机数类

Random

需求: 编写一个函数随机产生四位的验证码。

*/

public class Demo5 {

public static void main(String[] args) {

Random random = new Random();

int randomNum = random.nextInt(10)+1; //产生 的 随机数就是0-10之间

System.out.println("随机数:"+ randomNum);

char[] arr = {'中','国','传','a','Q','f','B'};

StringBuilder sb = new StringBuilder();

Random random1 = new Random();

//需要四个随机数,通过随机数获取字符数组中的字符,

for(int i  = 0 ; i< 4 ; i++){

int index = random1.nextInt(arr.length);  //产生的 随机数必须是数组的索引值范围之内的。

sb.append(arr[index]);

}

System.out.println("验证码:"+ sb);

}

}

/*

随机数:2

验证码:传aB传

*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值