Java实训课7
来源:互联网 发布:怎么分辨淘宝客 编辑:程序博客网 时间:2024/06/11 23:44
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。
StringBuffer和String运行速度比较,StringBuffer是String运行速度的几百倍;
Date时间类:表示特定的瞬间精确到毫秒
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateCalendar {
public static void main(String[] args) {
Date d=new Date();
System.out.println(d.toString());
System.out.println(d.toLocaleString());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss SSS");
String str=sdf.format(d);
System.out.println(str);
}
}
Calendar类:抽象类不能直接new关键字创建需要借助该类提供的静态方法getInstance()来获得一个Calendar对象
为了更加便捷的对日期进行操作,Calendar类对YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
import java.util.Calendar; 4 5 import org.junit.Before; 6 import org.junit.Test; 7 8 public class CalendarDemo { 9 Calendar calendar = null;10 11 @Before12 public void test() {13 calendar = Calendar.getInstance();14 }15 16 // 基本用法,获取年月日时分秒星期17 @Test18 public void test1() {19 // 获取年20 int year = calendar.get(Calendar.YEAR);21 22 // 获取月,这里需要需要月份的范围为0~11,因此获取月份的时候需要+1才是当前月份值23 int month = calendar.get(Calendar.MONTH) + 1;24 25 // 获取日26 int day = calendar.get(Calendar.DAY_OF_MONTH);27 28 // 获取时29 int hour = calendar.get(Calendar.HOUR);30 // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时表示31 32 // 获取分33 int minute = calendar.get(Calendar.MINUTE);34 35 // 获取秒36 int second = calendar.get(Calendar.SECOND);37 38 // 星期,英语国家星期从星期日开始计算39 int weekday = calendar.get(Calendar.DAY_OF_WEEK);40 41 System.out.println("现在是" + year + "年" + month + "月" + day + "日" + hour42 + "时" + minute + "分" + second + "秒" + "星期" + weekday);43 }44 45 // 一年后的今天46 @Test47 public void test2() {48 // 同理换成下个月的今天calendar.add(Calendar.MONTH, 1);49 calendar.add(Calendar.YEAR, 1);50 51 // 获取年52 int year = calendar.get(Calendar.YEAR);53 54 // 获取月55 int month = calendar.get(Calendar.MONTH) + 1;56 57 // 获取日58 int day = calendar.get(Calendar.DAY_OF_MONTH);59 60 System.out.println("一年后的今天:" + year + "年" + month + "月" + day + "日");61 }62 63 // 获取任意一个月的最后一天64 @Test65 public void test3() {66 // 假设求6月的最后一天67 int currentMonth = 6;68 // 先求出7月份的第一天,实际中这里6为外部传递进来的currentMonth变量69 // 170 calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);71 72 calendar.add(Calendar.DATE, -1);73 74 // 获取日75 int day = calendar.get(Calendar.DAY_OF_MONTH);76 77 System.out.println("6月份的最后一天为" + day + "号");78 }79 80 // 设置日期81 @Test82 public void test4() {83 calendar.set(Calendar.YEAR, 2000);84 System.out.println("现在是" + calendar.get(Calendar.YEAR) + "年");85 86 calendar.set(2008, 8, 8);87 // 获取年88 int year = calendar.get(Calendar.YEAR);89 90 // 获取月91 int month = calendar.get(Calendar.MONTH);92 93 // 获取日94 int day = calendar.get(Calendar.DAY_OF_MONTH)
System.out.println("现在是" + year + "年" + month + "月" + day + "日"); } }
SimpleDateFormat日历格式化可以对Date及字符串进行分析并在他们之间相互转换,它允许格式化,语法分析,标准化
- Java实训课7
- Java 7
- JAVA | 7
- JAVA安卓第三天实训课
- JAVA实训课第四天
- Java实训课1
- Java实训课2
- Java实训课3
- Java实训课4
- Java实训课5
- Java实训课6
- Java实训课8
- Java实训课9
- java第7天代码 (java异常)
- [Java] 学习笔记整理 [java 前 7]
- java 7 /java.nio.file 文件监控!!!
- java的数据类型(java 7更新)
- JAVA虚拟机规范 java SE 7 读书笔记
- php trait学习笔记
- BZOJ 3629-聪明的燕姿 【数学杂题】
- 第三周——项目三—求集合并集
- 自定义控件---添加子控件错误
- Drupal8 minimal Mode add comment form
- Java实训课7
- 操作系统---进程的死锁
- include动作与include指令的区别
- loadrunner Web_类函数之web_reg_save_param()
- spring mvc 拦截器 拦截路径
- 矩阵乘法的一些应用(完全版)
- 从jar包中读取资源文件
- ASP.NET Core默认提供的这个跨平台的服务器KestrelServer
- Hive之 hive架构