直接看例子:

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale; public class CalendarTest
{
public static void main(String[] args)
{
GregorianCalendar d = new GregorianCalendar();
String[] weekdayNames = new DateFormatSymbols(Locale.US).getShortWeekdays();
String[] monthsNames = new DateFormatSymbols(Locale.US).getShortMonths(); int today = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("Today is %2d in the month.%n", today); int month = d.get(Calendar.MONTH);
System.out.printf("This month is %4s.%n", monthsNames[month]); d.set(Calendar.DAY_OF_MONTH, 1);
System.out.printf("The first day of this month is %d.%n", d.get(Calendar.DAY_OF_MONTH)); int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.printf("The first week day of this month is %4s.%n", weekdayNames[weekday]); int firstDayOfWeek = d.getFirstDayOfWeek();
System.out.printf("The first week day of this week is %4s.%n", weekdayNames[firstDayOfWeek]); int indent = 0;
while (weekday != firstDayOfWeek)
{
indent++; // 当月开始日期减1天,星期自然也就减1天,一直减到星期天
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
System.out.printf("Today - Monday = %d.%nThe day is %4s now.%n", indent, weekdayNames[weekday]);
System.out.printf("The day is %4s %4s now.%n",
monthsNames[d.get(Calendar.MONTH)],
d.get(Calendar.DAY_OF_MONTH)); System.out.println(""); // 打印星期名称,这里采用美式日期格式,从周日开始
do
{
// 从下标为1开始取,美式星期下标为1取到的是周日
System.out.printf("%4s", weekdayNames[weekday]); // 上面日期已经减到星期天了,所以现在又从星期天开始加回去,下标从1开始加到7再变回1
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
} while (weekday != firstDayOfWeek);
System.out.println(); // 当月开始的日期距离星期天有几天,就需要补打几个空
for (int i = 1; i <= indent; i++)
{
System.out.printf("%4s", "");
} d.set(Calendar.DAY_OF_MONTH, 1);
do
{
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day); // 到当前日期标*,否则打个空格拉开距离
if (day == today)
{
System.out.print("*");
}
else
{
System.out.print(" ");
} // 日期加1,直到月末
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK); // 日期到周六换行
if (weekday == firstDayOfWeek)
{
System.out.println();
}
} while (d.get(Calendar.MONTH) == month); if (weekday != firstDayOfWeek)
{
System.out.println();
}
} }

  输出:

Today is 23 in the month.
This month is Aug.
The first day of this month is 1.
The first week day of this month is Wed.
The first week day of this week is Sun.
Today - Monday = 3.
The day is Sun now.
The day is Jul 29 now. Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23* 24 25
26 27 28 29 30 31

java的日期的更多相关文章

  1. [转载]Java 8 日期&时间 API

    Java 8 日期和时间 声明 本文转自http://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant,以mark ...

  2. 【转】JAVA 8 日期/时间(Date Time)API指南

    前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...

  3. 记一个JAVA关于日期的坑

    JAVA解析日期格式代码,之前一直写成:“yyyy-MM-dd hh:mm”,比如"2016-01-18 11:00"."2016-01-18 15:00"都可 ...

  4. java获取日期之间的差异

    转载请注明出处.谢谢http://blog.csdn.net/harryweasley/article/details/42121485 当想到要计算差值.我们肯定想的是"2014.12.1 ...

  5. java 获取日期的几天前,几个月前和几年前

    java 获取日期的几天前,几个月前和几年前. package bys.utils; import java.util.Date; /** * Created by toutou on 2015/3/ ...

  6. Java 8 日期时间 API

    转自:https://www.runoob.com/java/java8-datetime-api.html Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与 ...

  7. JAVA 8 日期工具类

    JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...

  8. Java 8 新特性-菜鸟教程 (8) -Java 8 日期时间 API

    Java 8 日期时间 API Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理. 在旧版的 Java 中,日期时间 API 存在诸多问题,其中有: ...

  9. Java Date 日期 时间 相关方法

    DateTools.java import java.text.SimpleDateFormat; import java.util.Date; /** * 日期操作类 */ public class ...

  10. Java 8 – 日期和时间实用技巧

    当你开始使用Java操作日期和时间的时候,会有一些棘手.你也许会通过System.currentTimeMillis() 来返回1970年1月1日到今天的毫秒数.或者使用Date类来操作日期:当遇到加 ...

随机推荐

  1. 个人知识管理系统Version1.0开发记录(03)

    demo  设 计 一个知识点demo,在数据库和用户界面的互动事件.分三个层次,数据存储,数据方法工具,数据呈现界面.这一次先完成数据存储,按以下逻辑实现.工具:eclipse,oracle数据库, ...

  2. 打开Eclipse提示“The default workspace “xxxx” is in use or cannot be created Please choose a different one“

    原因:出现这种情况一般是workspace的配置文件中出现了.lock文件(workspace/.metadata/.lock),锁定了workspace.把.lock文件删除即可. 如果该文件不能删 ...

  3. navicat for mysql 导入SQL Server显示中文乱码解决办法

    解决方法是在navicat里右击一个连接,选择连接属性,切换到高级选项卡,去掉“使用mysql字符集”前的对勾,在编码里选择utf-8

  4. C++复习5.指针数组字符串

    C/C++ 指针.数组和字符串 本次学习指针.数组.字符串.引用的内存映像. 1.指针 指针的本质:可以执行的程序是由指令.数据和地址组成的.当CPU访问内存单元的时候,不论是读取还是写入,首先要把内 ...

  5. MVC DateTime 字段 EditTime 必须是日期模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式

    ASP.NET MVC 中model含有DateTime类型的字段 更新字段时提示:字段 EditTime必须是日期,. 但是明明填入的是日期还是给出这个提示, 看有的博客说那是因为日期形式错了,如果 ...

  6. poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 138526   Accepted: 33859 ...

  7. ZOJ 2965 Accurately Say "CocaCola"!(预处理)

    Accurately Say "CocaCola"! Time Limit: 2 Seconds      Memory Limit: 65536 KB In a party he ...

  8. 2018.11.14 Chopin’s

    The lineaments of Chopin’s肖邦 short, dramatic life are familiar to most classical-music enthusiasts. ...

  9. New Concept English three(15)

    31w/m 43 Children always appreciate small gifts of money. Father, of course, provides a regular supp ...

  10. VERIFY DATABASE 正在异常终止。 (Microsoft SQL Server,错误: 3169)

    1.错误描述 标题: Microsoft SQL Server Management Studio ------------------------------ 备份介质验证失败: [文件: D:\S ...