本文主要是介绍获取当前时间,及当月日历,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;//当下时间的日历
public class MyCalendar {public static void main(String[] args) {GregorianCalendar now = new GregorianCalendar();Date date = new Date();System.out.println(date.toString());now.setTime(date);int today = now.get(Calendar.DAY_OF_MONTH);// day 16int month = now.get(Calendar.MONTH);// month 0System.out.println( " today:"+today +" \n month:"+ month );now.set(Calendar.DAY_OF_MONTH, 1);//该月第一天 day置为1int week = now.get(Calendar.DAY_OF_WEEK);// now 是一周的第几天System.out.println( " week: "+week );System.out.println(" Sun Mon Tue Wed Thu Fri Sat");for (int i = Calendar.SUNDAY; i < week; i++) {System.out.print(" ");//找到当月1号的位置}while (now.get(Calendar.MONTH) == month) {int day = now.get(Calendar.DAY_OF_MONTH);//从1号开始if (day < 10) {if (day == today)System.out.print(" -" + day + "- ");elseSystem.out.print(" " + day + " ");} else {if (day == today)System.out.print("-" + day + "- ");elseSystem.out.print(" " + day + " ");}if (week == Calendar.SATURDAY) {// 周六换行System.out.println();}now.add(Calendar.DAY_OF_MONTH, 1);week = now.get(Calendar.DAY_OF_WEEK);}}}
这篇关于获取当前时间,及当月日历的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!