Java学习笔记(十一)日期日历类和随机类等
2014-08-09 11:39
344 查看
1.Date类:包含一长整型数据
Date date = new Date();
system.out.println(date.getTime()); //获取的是毫秒数
2. Calendar类:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,1);//获取前一年时间:
下面程序完成按指定格式显示当前时间
aaabbbccc
1407554937622
11-28-星期六-八月-09-2014
下面程序获取一个日期的年月日
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd-EEEE");
Date date = new Date();
System.out.println(simpleDateFormat.format(date));
// 更常见的方法
Calendar cal = Calendar.getInstance(); // 抽象类不能直接实例化
String result = cal.get(Calendar.YEAR) + "年"
+ (cal.get(Calendar.MONTH) + 1) + "月"
+ cal.get(Calendar.DAY_OF_MONTH) + "日";
System.out.println(result);
// 将字符串转化为日期
String str1 = "05-18-2014";
SimpleDateFormat sDateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date1 = sDateFormat.parse(str1);
System.out.println(sDateFormat.format(date1));
// 显示时间日期时分秒
Date date2 = new Date();
SimpleDateFormat sDateFormat1 = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
System.out.println(sDateFormat1.format(date2));
// 获取本月1号是该年的星期几
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DATE, 1);
System.out.println(cal1.get(Calendar.WEEK_OF_YEAR));
}
}
运行结果为:
2014-08-09-星期六
2014年8月9日
05-18-2014
2014-08-09 11:33:28
313. Random类:
常见的方法有:
public Random(); //构造函数
public void nextBytes(byte[] bytes); //生成随机数据的字节数组,存放在byte[]
public int nextInt(); //范围[0~n)整数
public long nextLong(); //long范围随机数
public float nextFloat(); //范围为0.0f~1.0f
public double nextDouble(); //范围为[0.0d~1.0d)等
程序说明:
import java.util.Random;
public class RandomNumber {
public static void main(String[] args) {
Random rand1 = new Random();
byte[] b = new byte[5];
rand1.nextBytes(b); //产生5个随机的自己额数据
for(int i=0;i<b.length;i++)
System.out.print(b[i]+"\t");
System.out.println();
for(int i =0;i<5;i++){
System.out.print(rand1.nextInt(100)+"\t");
}
System.out.println();
for(int i=0;i<3;i++){
System.out.print(rand1.nextDouble()+"\t");
}
System.out.println();
}
}
运行结果为:
11 75 -117 107 -37
90 83 53 31 70
0.7060466247360981 0.16247514498972482 0.5271465262376611
下面的程序演示了Java常用的三种产生随机数的方法:
import java.util.Random;
public class Demo02 {
public static void main(String[] args) {
// 随机数的使用
//(1)使用Math类中的random
Double r1 = Math.random();
System.out.println(r1);
//(2)使用Random类
Random r2 = new Random();
int result = r2.nextInt(100); //产生0-100之间的随机数
System.out.println(result);
//使用随机种子
Random r3 = new Random(1000);
int result2 = r3.nextInt(100);
System.out.println(result2);
//(3)当前时间作为随机种子
Random r4 = new Random(System.currentTimeMillis());
int result3 = r4.nextInt(100);
System.out.println(result3);
}
}
运行结果为:
0.6787078701919754
57
87
94
4. 其他常见格式类
(1)MessageFormat类
import java.text.MessageFormat;
public class Demo03 {
/**
* @param args
*/
public static void main(String[] args) {
// 使用MessageFormat类
String sql = MessageFormat.format(
"select student * from where stuNu = {0} and password = {1} ",
1001, 123456);
System.out.println(sql);
// MessageFormat类的缺点
String userName = "admin";
String passWord = "123456";
// 在此注意占位符有无单引号的区别
String sql2 = MessageFormat
.format("select student * where username = \"{0}\" and passwor = \'{1}\'",
userName, passWord);
System.out.println(sql2);
// sql语句要求参数用单引号引出,因此解决上述问题的方法为:
String sql3 = String.format(
"select student * where username = '%s' and passwor = '%s'",
userName, passWord);
System.out.println(sql3);
}
}运行结果为:
select student * from where stuNu = 1,001 and password = 123,456
select student * where username = "admin" and passwor = {1}
select student * where username = 'admin' and passwor = '123456'
(2)NumberFormat类:
Date date = new Date();
system.out.println(date.getTime()); //获取的是毫秒数
2. Calendar类:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,1);//获取前一年时间:
下面程序完成按指定格式显示当前时间
import java.text.SimpleDateFormat; import java.util.Date; public class Demo04 { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("aaa"); sb.append("bbb"); sb.append("ccc"); String s = sb.toString(); System.out.println(s); SimpleDateFormat bartDateFormat = new SimpleDateFormat("hh-mm-EEEE-MMMM-dd-yyyy"); Date date = new Date(); System.out.println(date.getTime()); System.out.println(bartDateFormat.format(date)); } }运行结果为:
aaabbbccc
1407554937622
11-28-星期六-八月-09-2014
下面程序获取一个日期的年月日
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd-EEEE");
Date date = new Date();
System.out.println(simpleDateFormat.format(date));
// 更常见的方法
Calendar cal = Calendar.getInstance(); // 抽象类不能直接实例化
String result = cal.get(Calendar.YEAR) + "年"
+ (cal.get(Calendar.MONTH) + 1) + "月"
+ cal.get(Calendar.DAY_OF_MONTH) + "日";
System.out.println(result);
// 将字符串转化为日期
String str1 = "05-18-2014";
SimpleDateFormat sDateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date1 = sDateFormat.parse(str1);
System.out.println(sDateFormat.format(date1));
// 显示时间日期时分秒
Date date2 = new Date();
SimpleDateFormat sDateFormat1 = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
System.out.println(sDateFormat1.format(date2));
// 获取本月1号是该年的星期几
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DATE, 1);
System.out.println(cal1.get(Calendar.WEEK_OF_YEAR));
}
}
运行结果为:
2014-08-09-星期六
2014年8月9日
05-18-2014
2014-08-09 11:33:28
313. Random类:
常见的方法有:
public Random(); //构造函数
public void nextBytes(byte[] bytes); //生成随机数据的字节数组,存放在byte[]
public int nextInt(); //范围[0~n)整数
public long nextLong(); //long范围随机数
public float nextFloat(); //范围为0.0f~1.0f
public double nextDouble(); //范围为[0.0d~1.0d)等
程序说明:
import java.util.Random;
public class RandomNumber {
public static void main(String[] args) {
Random rand1 = new Random();
byte[] b = new byte[5];
rand1.nextBytes(b); //产生5个随机的自己额数据
for(int i=0;i<b.length;i++)
System.out.print(b[i]+"\t");
System.out.println();
for(int i =0;i<5;i++){
System.out.print(rand1.nextInt(100)+"\t");
}
System.out.println();
for(int i=0;i<3;i++){
System.out.print(rand1.nextDouble()+"\t");
}
System.out.println();
}
}
运行结果为:
11 75 -117 107 -37
90 83 53 31 70
0.7060466247360981 0.16247514498972482 0.5271465262376611
下面的程序演示了Java常用的三种产生随机数的方法:
import java.util.Random;
public class Demo02 {
public static void main(String[] args) {
// 随机数的使用
//(1)使用Math类中的random
Double r1 = Math.random();
System.out.println(r1);
//(2)使用Random类
Random r2 = new Random();
int result = r2.nextInt(100); //产生0-100之间的随机数
System.out.println(result);
//使用随机种子
Random r3 = new Random(1000);
int result2 = r3.nextInt(100);
System.out.println(result2);
//(3)当前时间作为随机种子
Random r4 = new Random(System.currentTimeMillis());
int result3 = r4.nextInt(100);
System.out.println(result3);
}
}
运行结果为:
0.6787078701919754
57
87
94
4. 其他常见格式类
(1)MessageFormat类
import java.text.MessageFormat;
public class Demo03 {
/**
* @param args
*/
public static void main(String[] args) {
// 使用MessageFormat类
String sql = MessageFormat.format(
"select student * from where stuNu = {0} and password = {1} ",
1001, 123456);
System.out.println(sql);
// MessageFormat类的缺点
String userName = "admin";
String passWord = "123456";
// 在此注意占位符有无单引号的区别
String sql2 = MessageFormat
.format("select student * where username = \"{0}\" and passwor = \'{1}\'",
userName, passWord);
System.out.println(sql2);
// sql语句要求参数用单引号引出,因此解决上述问题的方法为:
String sql3 = String.format(
"select student * where username = '%s' and passwor = '%s'",
userName, passWord);
System.out.println(sql3);
}
}运行结果为:
select student * from where stuNu = 1,001 and password = 123,456
select student * where username = "admin" and passwor = {1}
select student * where username = 'admin' and passwor = '123456'
(2)NumberFormat类:
import java.text.NumberFormat; public class Demo04 { public static void main(String[] args) { Double myNumber = 23323.3323232323; Double test = 0.3434; // getInstance() // 返回当前缺省语言环境的缺省数值格式。 String myString = NumberFormat.getInstance().format(myNumber); System.out.println(myString); // getCurrencyInstance()返回当前缺省语言环境的通用格式 myString = NumberFormat.getCurrencyInstance().format(myNumber); System.out.println(myString); // getNumberInstance() 返回当前缺省语言环境的通用数值格式。 myString = NumberFormat.getNumberInstance().format(myNumber); System.out.println(myString); // getPercentInstance() 返回当前缺省语言环境的百分比格式。 myString = NumberFormat.getPercentInstance().format(test); System.out.println(myString); // setMaximumFractionDigits(int) 设置数值的小数部分允许的最大位数。 // setMaximumIntegerDigits(int) 设置数值的整数部分允许的最大位数。 // setMinimumFractionDigits(int) 设置数值的小数部分允许的最小位数。 // setMinimumIntegerDigits(int) 设置数值的整数部分允许的最小位数. NumberFormat format = NumberFormat.getInstance(); format.setMinimumFractionDigits(3); format.setMaximumFractionDigits(5); format.setMaximumIntegerDigits(10); format.setMinimumIntegerDigits(0); System.out.println(format.format(2132323213.23266666666)); } }运行结果为:
23,323.332 ¥23,323.33 23,323.332 34% 2,132,323,213.23267
相关文章推荐
- java学习笔记十一——对象转型
- Java开发学习笔记之十一:开始JSP之旅
- [每日学习笔记][2012.08.03]使用Java理解程序逻辑(十一)
- Java菜鸟学习笔记--面向对象篇(十一):&abstract方法/类
- JAVA学习笔记(十一):enum的使用
- 11. JAVA常用类库 Part 2 (System类、日期操作类) ----- 学习笔记
- Java学习笔记——显示当前日期的三种方式
- java之jvm学习笔记十一(访问控制器)
- java 从零开始,学习笔记之基础入门<封装类>(十一)
- Java学习笔记07--日期操作类
- java中日期、日期格式、日期计算学习笔记(一)
- java学习笔记07--日期操作类
- java 设计模式学习笔记十一flyweight享元设计模式
- Java学习笔记-日期和时间
- Java基础学习笔记(十一)I/O 文件访问
- java中日期、日期格式、日期计算学习笔记(二)
- java之jvm学习笔记十一(访问控制器)
- JAVA学习笔记(十一)连接数据库
- Java学习笔记(十一)Collections framework之collection接口,iterator接口
- JAVA学习笔记(十一)- 重载和重写