使用Calendar 将当月日历打印出来
package com.date; import java.text.DateFormatSymbols;
import java.util.Calendar; import org.junit.Test; public class CalendarTest {
/**
* 打印当前月的日历
*/
@Test
public void test1(){ // GregorianCalendar d = new GregorianCalendar();
Calendar d = Calendar.getInstance();
// 获取 当前 日 和月
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH); // System.out.println(day+"\n"+month); //设置当前时间为 本月第一天
d.set(Calendar.DAY_OF_MONTH, 1);
//获取这一天是星期几
int weekday = d.get(Calendar.DAY_OF_WEEK); //获取当前地区 一周的第一天是星期几
int firstDayOfWeek = d.getFirstDayOfWeek(); // System.out.println(weekday+"\n"+firstDayOfWeek); //补充当前月 第一个星期中的 缩进的天数
int indent =0;
while(weekday!=firstDayOfWeek){
indent ++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
} //打印星期
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
// System.out.println(Arrays.asList(weekdayNames).toString());
do{
System.out.printf("%4s",weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}while(weekday!=firstDayOfWeek); System.out.println(); //将第一行 缩进的星期 空出来
for(int i=0;i<indent;i++){
System.out.print(" ");
}
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(" ");
} //继续下一天
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); }
}
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAScAAACACAIAAADGVjj0AAAJnklEQVR4nO2d22GkOhBEScsERDomhBvFJEMw3I8Zr9G7JbqKwVPny+uHigYVA6y6mHYhvNm+v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" />
使用Calendar 将当月日历打印出来的更多相关文章
- Java Calendar实现控制台日历
public static void main(String[] args) throws IOException { //初始化日历对象 Calendar calendar = Calendar.g ...
- python之模块calendar(汇集了日历相关的操作)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #calendar日历模块 import calendar #3个大类: calendar.Calendar( ...
- 用最笨的方法实现java控制台日历打印
如果想用户自定义输入日期查询,可以通过Calendar的set方法和Scanner方法设置 Calendar类简单使用:https://blog.csdn.net/weixin_43670802/ar ...
- WPF 自定义Calendar样式(日历样式,周六周日红色显示)
一.WPF日历控件基本样式 通过Blend获取到Calendar需要设置的三个样式CalendarStyle.CalendarButtonStyle.CalendarDayButtonStyle.Ca ...
- oracle 当月日历的sql
select max(sun) sun, max(mon) mon, max(tue) tue, max(wed) wed, max(thu) thu, max(fri) fri, max(sat) ...
- Java中的Calendar日历用法详解
第一部分 Calendar介绍 public abstract class Calendar implements Serializable, Cloneable, Comparable<Cal ...
- 用java打印日历
来自<java核心技术卷一> /** * Created by wangbin10 on 2019/1/3. * 打印当月日历 */ public class CalendarTest { ...
- Oracle打印日历功能
Oracle用SQL打印日历 1.1 打印当月日历 , D, NULL)) SUN, , D, NULL)) MON, , D, NULL)) TUE, , D, NULL)) WED, , D, ...
- java之Date(日期)、Date格式化、Calendar(日历)
参考http://how2j.cn/k/date/date-date/346.html Date(日期) Date类 注意:是java.util.Date; 而非 java.sql.Date,此类是给 ...
随机推荐
- 深入理解PHP Opcode缓存原理
什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译,减少 ...
- DataTable的新建、查询、添加和修改
详细讲解了C#开发中数据类型DataTable的使用,包括新建DataTable,查询DataTable,在DataTable里添加记录,添加 列,修改列的属性以及修改某行某列的值的方法.同时说明了将 ...
- Cisco SDM
SDM连接方式:http+telnet / https+ssh 要使用SDM对CISCO设备实现集中式管理,必须在设备上键入如下命令: 步骤1: 要启用路由器的HTTP/HTTPS 服务器,请 ...
- POJ2533Longest Ordered Subsequence(DP)
http://poj.org/problem?id=2533 在经典不过的DP题目了.... #include <map> #include <set> #include &l ...
- hdoj 5386 Cover
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...
- 修改Android 程序的icon快捷方式图标和名称
在res/drawable-hdpi或res/drawable-ldpi或res/drawable-mdpi目录下,加下你要显示的图片,最好后缀是为.png的,然后修改AndroidManifest. ...
- myeclipse 8.5安装freemarker插件方法
1. 下载freemarker最新版本,目前本人下载时最新版本是:freemarker-ide-0.9.14.zip, ide版本的freemarker.jar版本太低,可以替换成高级版本,并修改me ...
- Junit4单元测试
在Eclipse中使用JUnit4进行单元测试(初级篇) 在Eclipse中使用JUnit4进行单元测试(中级篇) 在Eclipse中使用JUnit4进行单元测试(高级篇)
- [读书笔记]ASP.NET的URL路由引擎
作用 一般的URL: 举例:http://www.myapp.com/app.aspx?id=2&sessionid=29320xafafa02fa0zga0g8a0z 缺点: 不美观,不清晰 ...
- oracle中substr函数的用法
1.substr(string string, int a, int b) 参数1:string 要处理的字符串 参数2:a 截取字符串的开始位置(起始位置是0) 参数3:b 截取的字符串的长度(而不 ...