获取本周一
public static Date getNowWeekMonday(Date date) {Calendar cal = CalendargetInstance();
calsetTime(date);
caladd(CalendarDAY_OF_MONTH, -1); //解决周日会出现 并到下一周的情况
calset(CalendarDAY_OF_WEEK, CalendarMONDAY);
303 return calgetTime();
}
获取上周一
public static Date getLastWeekMonday(Date date) {Date a = DateUtilsaddDays(date, -1);
Calendar cal = CalendargetInstance();
calsetTime(a);
caladd(CalendarWEEK_OF_YEAR, -1);// 一周
calset(CalendarDAY_OF_WEEK, CalendarMONDAY);
return calgetTime();
}
获取上周日
public static Date getLastWeekSunday(Date date) {
Date a = DateUtilsaddDays(date, -1);
Calendar cal = CalendargetInstance();
calsetTime(a);
calset(CalendarDAY_OF_WEEK, 1);
return calgetTime();
}
代码里面有用到 lapachecommon-ang包 你需要下载下 就可以使用
//1将字符串用split切割得到年月日组成的数组 String s2="2011-11-11 11:11:11";//2Calendar对象的获得,abstract并且构造函数是protected //本地时区和本地的习惯,系统日期 Calendar calendar=CalendargetInstance();//3将Calendar转换成输入的日期 用calendar对象的set(Calendar对应常量(如YEAR等),输入的对应值)方法设值//4获得判断用的值 //获得年份 int year=calendarget(CalendarYEAR); //获得这个月最多的天数 int maxDay=todaygetActualMaximum(CalendarDATE); //获得当前日期是一周中的第几天,注意这个数不代表星期几而是你电脑上日历的第几列 int weekDay=calendarget(CalendarDAY_OF_WEEK); }好了就这么多吧,有这些差不多了,还有什么继续问哈
代码实现如下:
package test01;
import javautilCalendar;
/
@author 码灵
20170905
/
public class GetDay {
public static void main(String[] args) {
int currentMaxDays = getCurrentMonthDay();
int maxDaysByDate = getDaysByYearMonth(2017, 9);
Systemoutprintln("本月天数:" + currentMaxDays);
Systemoutprintln("2017年9月天数:" + maxDaysByDate);
}
/
获取当月的 天数
/
public static int getCurrentMonthDay() {
Calendar a = CalendargetInstance();
aset(CalendarDATE, 1);
aroll(CalendarDATE, -1);
int maxDate = aget(CalendarDATE);
return maxDate;
}
/
根据年 月 获取对应的月份 天数
/
public static int getDaysByYearMonth(int year, int month) {
Calendar a = CalendargetInstance();
aset(CalendarYEAR, year);
aset(CalendarMONTH, month - 1);
aset(CalendarDATE, 1);
aroll(CalendarDATE, -1);
int maxDate = aget(CalendarDATE);
return maxDate;
}
}
public class ReadFromFile {
/
以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。
/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = inread()) != -1) {
Systemoutwrite(tempbyte);
}
inclose();
} catch (IOException e) {
eprintStackTrace();
return;
}
try {
Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFileshowAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = inread(tempbytes)) != -1) {
Systemoutwrite(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1printStackTrace();
} finally {
if (in != null) {
try {
inclose();
} catch (IOException e1) {
}
}
}
}
/
以字符为单位读取文件,常用于读文本,数字等类型的文件
/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = readerread()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
Systemoutprint((char) tempchar);
}
}
readerclose();
} catch (Exception e) {
eprintStackTrace();
}
try {
Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = readerread(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempcharslength)
&& (tempchars[tempcharslength - 1] != '\r')) {
Systemoutprint(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
Systemoutprint(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1printStackTrace();
} finally {
if (reader != null) {
try {
readerclose();
} catch (IOException e1) {
}
}
}
}
/
以行为单位读取文件,常用于读面向行的格式化文件
/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
Systemoutprintln("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = readerreadLine()) != null) {
// 显示行号
Systemoutprintln("line " + line + ": " + tempString);
line++;
}
readerclose();
} catch (IOException e) {
eprintStackTrace();
} finally {
if (reader != null) {
try {
readerclose();
} catch (IOException e1) {
}
}
}
}
/
随机读取文件内容
/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
Systemoutprintln("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFilelength();
// 读文件的起始位置
int beginIndex = (fileLength > 4) 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFileseek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFileread(bytes)) != -1) {
Systemoutwrite(bytes, 0, byteread);
}
} catch (IOException e) {
eprintStackTrace();
} finally {
if (randomFile != null) {
try {
randomFileclose();
} catch (IOException e1) {
}
}
}
}
/
显示输入流中还剩的字节数
/
private static void showAvailableBytes(InputStream in) {
try {
Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemptxt";
ReadFromFilereadFileByBytes(fileName);
ReadFromFilereadFileByChars(fileName);
ReadFromFilereadFileByLines(fileName);
ReadFromFilereadFileByRandomAccess(fileName);
}
}
给你个例子,这个写的不好,回去再给你写个
import javautilCalendar;
public class $ {
public static void main(String _) {
String str = test(2012, 6);
Systemoutprintln(str);
}
private static String test(int year, int month) {
Calendar c = CalendargetInstance();
cset(CalendarYEAR, year);
cset(CalendarMONTH, month - 1);
String str = "日\t一\t二\t三\t四\t五\t六\r";
int day = getDay(year, month - 1);
cset(CalendarDAY_OF_MONTH, 1);
str += getStartWeizhi(c);
for (int i = 2; i <= day; i++) {
cset(CalendarDAY_OF_MONTH, i);
if (cget(CalendarDAY_OF_WEEK) == CalendarSATURDAY) {
str += i + "\r";
} else {
str += i + "\t";
}
}
return str;
}
private static int getDay(int year, int month) {
int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month != 2) {
return DAYS[month];
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
}
return DAYS[month];
}
private static String getStartWeizhi(Calendar c) {
int idx = cget(CalendarDAY_OF_WEEK);
StringBuffer buf = new StringBuffer();
for (int i = 0; i < idx - 1; i++) {
bufappend(" \t");
}
if (CalendarSATURDAY == idx) {
bufappend(cget(CalendarDAY_OF_MONTH) + "\r");
} else {
bufappend(cget(CalendarDAY_OF_MONTH) + "\t");
}
return buftoString();
}
}
结果:
日 一 二 三 四 五 六
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
==我去给你写。等下贴给你。
/
传入一个月份,得到这个月份所有的周三,周四将其打印出来。
@param month 参数指定的月份
@throws Exception
/
private void getwendsor(int month) throws Exception
{
if(month<1||month>12)
throw new Exception("请指定一个合法的月份。");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=CalendargetInstance();
calset(CalendarMONTH,month-1);
calset(CalendarDAY_OF_MONTH, 1);
int day=1;
do
{
//Systemoutprintln(sdfformat(calgetTime())+" "+calget(CalendarDAY_OF_WEEK)+" "+CalendarWEDNESDAY);
if(calget(CalendarDAY_OF_WEEK)==CalendarWEDNESDAY)
Systemoutprintln(month+"月份的"+sdfformat(calgetTime())+ "是周三");
if(calget(CalendarDAY_OF_WEEK)==CalendarTHURSDAY)
Systemoutprintln(month+"月份的"+sdfformat(calgetTime())+ "是周四");
day++;
calset(CalendarDAY_OF_MONTH, day);
}while(calget(CalendarMONTH)+1==month);
}
这个方法是单独定义的,所以你可以随意调用,并且得到任意一个月份的周三或者周四。只需要在调用的时候指定您要查看的月份即可。
如果有疑问欢迎交流。呵呵
MainFramejava是显示日历程序,Clockjava是日历计算程序(可以不要)。
编译后运行MainFrame这个类即可。
swing窗口显示万年历,jdk14以上环境编译运行。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 package orgtest;import javaawtBorderLayout;import javaawtColor;import javaawtGridLayout;import javaawtToolkit;import javaawteventActionEvent;import javaawteventActionListener;import javasqlDate;import javautilCalendar;import javaxswingJComboBox;import javaxswingJFrame;import javaxswingJLabel;import javaxswingJPanel;public class MainFrame extends JFrame { private static final long serialVersionUID = -306484324260972141L; JPanel panel = new JPanel(new BorderLayout()); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(new GridLayout(7, 7)); JPanel panel3 = new JPanel(); JLabel[] label = new JLabel[49]; JLabel y_label = new JLabel("年份"); JLabel m_label = new JLabel("月份"); JComboBox com1 = new JComboBox(); JComboBox com2 = new JComboBox(); int re_year, re_month, x_size, y_size; String year_num; Calendar now = CalendargetInstance(); // 实例化Calendar MainFrame() { super("万年历"); setSize(300, 350); x_size = (int) (ToolkitgetDefaultToolkit()getScreenSize()getWidth()); y_size = (int) (ToolkitgetDefaultToolkit()getScreenSize()getHeight()); setLocation((x_size - 300) / 2, (y_size - 350) / 2); setDefaultCloseOperation(JFrameEXIT_ON_CLOSE); panel1add(y_label); panel1add(com1); panel1add(m_label); panel1add(com2); for (int i = 0; i < 49; i++) { label[i] = new JLabel("", JLabelCENTER);// 将显示的字符设置为居中 panel2add(label[i]); } panel3add(new Clock(this)); paneladd(panel1, BorderLayoutNORTH); paneladd(panel2, BorderLayoutCENTER); paneladd(panel3, BorderLayoutSOUTH); panelsetBackground(Colorwhite); panel1setBackground(Colorwhite); panel2setBackground(Colorwhite); panel3setBackground(Colorwhite); Init(); com1addActionListener(new ClockAction()); com2addActionListener(new ClockAction()); setContentPane(panel); setVisible(true); setResizable(false); } class ClockAction implements ActionListener { public void actionPerformed(ActionEvent arg0) { int c_year, c_month, c_week; c_year = IntegerparseInt(com1getSelectedItem()toString()); // 得到当前所选年份 c_month = IntegerparseInt(com2getSelectedItem()toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11 c_week = use(c_year, c_month); // 调用函数use,得到星期几 Resetday(c_week, c_year, c_month); // 调用函数Resetday } } public void Init() { int year, month_num, first_day_num; String log[] = { "日", "一", "二", "三", "四", "五", "六" }; for (int i = 0; i < 7; i++) { label[i]setText(log[i]); } for (int i = 0; i < 49; i = i + 7) { label[i]setForeground(Colorred); // 将星期日的日期设置为红色 } for (int i = 6; i < 49; i = i + 7) { label[i]setForeground(Colorgreen);// 将星期六的日期设置为绿色 } for (int i = 1; i < 10000; i++) { com1addItem("" + i); } for (int i = 1; i < 13; i++) { com2addItem("" + i); } month_num = (int) (nowget(CalendarMONTH)); // 得到当前时间的月份 year = (int) (nowget(CalendarYEAR)); // 得到当前时间的年份 com1setSelectedIndex(year - 1); // 设置下拉列表显示为当前年 com2setSelectedIndex(month_num); // 设置下拉列表显示为当前月 first_day_num = use(year, month_num); Resetday(first_day_num, year, month_num); } public int use(int reyear, int remonth) { int week_num; nowset(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天 week_num = (int) (nowget(CalendarDAY_OF_WEEK));// 得到第一天的星期 return week_num; } @SuppressWarnings("deprecation") public void Resetday(int week_log, int year_log, int month_log) { int month_day_score; // 存储月份的天数 int count; month_day_score = 0; count = 1; Date date = new Date(year_log, month_log + 1, 1); // now Calendar cal = CalendargetInstance(); calsetTime(date); caladd(CalendarMONTH, -1); // 前个月 month_day_score = calgetActualMaximum(CalendarDAY_OF_MONTH);// 最后一天 for (int i = 7; i < 49; i++) { // 初始化标签 label[i]setText(""); } week_log = week_log + 6; // 将星期数加6,使显示正确 month_day_score = month_day_score + week_log; for (int i = week_log; i < month_day_score; i++, count++) { label[i]setText(count + ""); } } public static void main(String[] args) { JFramesetDefaultLookAndFeelDecorated(true); new MainFrame(); }}
12345678910111213141516171819202122232425262728293031323334353637383940414243 package orgtest;import javaawtColor;import javautilCalendar;import javaawtCanvas;import javaawtFont;import javaawtGraphics;import javatextSimpleDateFormat;public class Clock extends Canvas implements Runnable{ private static final long serialVersionUID = 3660124045489727166L; MainFrame mf; Thread t; String time; public Clock(MainFrame mf){ thismf=mf; setSize(280,40); setBackground(Colorwhite); t=new Thread(this);//实例化线程 tstart(); //调用线程 } public void run(){ while(true){ try{ Threadsleep(1000);//休眠1秒钟 }catch(InterruptedException e){ Systemoutprintln("异常"); } thisrepaint(100); } } public void paint(Graphics g){ Font f=new Font("宋体",FontBOLD,16); SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型 Calendar now=CalendargetInstance(); time=SDFformat(nowgetTime()); //得到当前日期和时间 gsetFont(f); gsetColor(Colororange); gdrawString(time,45,25); }}
以上就是关于java 如何获取 上周一日期,上周末日期,本周一日期。全部的内容,包括:java 如何获取 上周一日期,上周末日期,本周一日期。、怎样利用java程序输出一个系统日期、java中 如何获取当月的天数,指定日期的月份天数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)