SimpleDateFormat类和Random类
来源:互联网 发布:知乎扒皮密子君催吐 编辑:程序博客网 时间:2024/05/29 15:02
SimpleDateFormat
SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调试的问题,因为 DateFormat 和 SimpleDateFormat 类不都是线程安全的,在多线程环境下调用 format() 和 parse() 方法应该使用同步代码来避免问题
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。
public class SimpleDateFormat extends DateFormat
public class FormatDateTime { public static void main(String[] args) { SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString() SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E "); SimpleDateFormat myFmt4=new SimpleDateFormat( "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区"); Date now=new Date(); System.out.println(myFmt.format(now)); System.out.println(myFmt1.format(now)); System.out.println(myFmt2.format(now)); System.out.println(myFmt3.format(now)); System.out.println(myFmt4.format(now)); System.out.println(now.toGMTString()); System.out.println(now.toLocaleString()); System.out.println(now.toString()); } }
效果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
Random
关于Math类中的random方法
其实在Math类中也有一个random方法,该random方法的工作是生成一个[0,1.0)区间的随机小数。
通过阅读Math类的源代码可以发现,Math类中的random方法就是直接调用Random类中的nextDouble方法实现的。
只是random方法的调用比较简单,所以很多程序员都习惯使用Math类的random方法来生成随机数字
示例
//java 随机数生成类 Random public class RandomTest { public static void main(String[] args){ //先建一个对象 //可以提供一个随机数种子,默认是系统时间 Random rand = new Random(); //布尔型随机数 System.out.println("rand.nextBoolean"+rand.nextBoolean()); byte[] buffer = new byte[16]; //为一个byte数组赋随机数 rand.nextBytes(buffer); System.out.println(Arrays.toString(buffer)); //生成0.0~1.0之间的伪随机数 System.out.println("rand.nextDouble:"+rand.nextDouble()); //生成0.0~1.0之间的伪随机数 System.out.println("rand.nextFloat:"+rand.nextFloat()); //生成一个整数取值范围内的伪随机数 System.out.println("rand.nextInt:"+rand.nextInt()); //生成一个0~26之间的伪随机数 System.out.println("rand.nextInt(26):"+rand.nextInt(26)); //生成一个long整数取值范围的伪随机数 System.out.println("rand.nextLong():"+rand.nextLong()); } }
参考博客:
Random类的使用
http://www.cnblogs.com/Fskjb/archive/2009/08/29/1556417.html
http://blog.csdn.net/acm_lkl/article/details/43121983
- SimpleDateFormat类和Random类
- 14-正则表达式/Pattern和Matcher/Math类/Random类/System类/SimpleDateFormat类/Calendar类
- Date类和SimpleDateFormat类
- DateFormat类和SimpleDateFormat类
- Date、Calender和SimpleDateFormat类
- 类 SimpleDateFormat
- SimpleDateFormat类
- 类 SimpleDateFormat
- SimpleDateFormat类
- Math中的random和Random类
- Math和Random类
- java常用类库续1(日期操作类Date、Calendar、DateFormat、SimpleDateFormat、Math与Random类)
- java常用类库续1(日期操作类Date、Calendar、DateFormat、SimpleDateFormat、Math与Random类)
- 常用类 StringBuffer Scanner Random SimpleDateFormat Pattern ArrayList LinkedList Set Map
- day11 常用类 System Runtime Math Random Date DateFormat SimpleDateFormat Calendar
- JAVA基础复习十四-Math、Random、System、BigInteger、BigDecimal、Date、SimpleDateFormat类
- SimpleDateFormat类和Calendar类的使用
- Date、SimpleDateFormat日期类和Calender类
- CSS笔记——z-index
- C++ sizeof
- 简单的验证码
- OpenGL ES 2.0基本概念
- 算法系列——数值的整数次方
- SimpleDateFormat类和Random类
- Spring Boot +Shiro 简单介绍
- 开机logo
- 2017产业互联网CEO特训营导师培训 路演比拼!
- scikit-learn实现决策树
- BZOJ 4176 Lucas的数论
- 【dlib代码解读】人脸关键点检测器的训练
- POJ 1236:Network of Schools
- linux 6