A. java判读当天是否是本月第一天
importjava.util.Date;
importjava.util.Calendar;
importjava.text.SimpleDateFormat;
publicclasstest{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Datenow=newDate();
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy/MM/dd");//可以方便地修改日期格式
Stringcurr=dateFormat.format(now);
System.out.println("当前日期:"+curr);
Calendarc=Calendar.getInstance();//可以对每个时间域单独修改
intyear=c.get(Calendar.YEAR);
intmonth=c.get(Calendar.MONTH);
intdate=c.get(Calendar.DATE);
if(date==1)
System.out.println(curr+"是第一天");
else
System.out.println(curr+"不是第一天");
}
}
B. java 怎样获取每个月的第一天和最后一天
SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-dd");
//获取前月的第一天
Calendarcal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH,-1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
firstDay=format.format(cal_1.getTime());
System.out.println("-----1------firstDay:"+firstDay);
//获取前月的最后一天
Calendarcale=Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
lastDay=format.format(cale.getTime());
System.out.println("-----2------lastDay:"+lastDay);
//获取当前月第一天:
Calendarc=Calendar.getInstance();
c.add(Calendar.MONTH,0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
Stringfirst=format.format(c.getTime());
System.out.println("===============first:"+first);
//获取当前月最后一天
Calendarca=Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH,ca.getActualMaximum(Calendar.DAY_OF_MONTH));
Stringlast=format.format(ca.getTime());
System.out.println("===============last:"+last);
C. JAVA显示每月第一天星期几
Calendarcal=Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
System.out.println(cal.get(Calendar.YEAR)+""+(cal.get(Calendar.MONTH)+1)+""+cal.get(Calendar.DAY_OF_MONTH)+"");
inti=cal.get(Calendar.DAY_OF_WEEK);
String[]str={"","SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};
System.out.println(str[i]);
运行结果:
2016 7 1
FRIDAY
说明:
Calendar的getActualMinimum
(int field)他传入的是一个int类型,这个方法的具体意思就是说根据你传入的参数代表的意思(年、月、周等)查询当前(年、月、周)拥有的最小值。
另外:
Calendar的getActualMaximum(int field)他传入的是一个int类型,这个方法的具体意思就是说根据你传入的参数代表的意思(年、月、周等)查询当前(年、月、周)拥有的最大值。
D. java怎么获取当前月的第一天和最后一天
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String args[]) {
System.out.println(getLastDayOfMonth(new Date()));
}
public static Date getLastDayOfMonth(Date sDate1) {
Calendar cDay1 = Calendar.getInstance();
cDay1.setTime(sDate1);
final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
Date lastDate = cDay1.getTime();
lastDate.setDate(lastDay);
return lastDate;
}
}
E. Java输出每月的第一天
int firsday1=0;
int firsday2=0;
int firsday3=0;
int firsday4=0;
int firsday5=0;
int firsday6=0;
int firsday7=0;
int firsday8=0;
int firsday9=0;
int firsday10=0;
int firsday11=0;
int firsday12=0;
单词写错了 都少了个t firs(t)day
在下面用的时候都是firstday1,应该把下面的t都去掉把
下面还有很多变量都写错了,变量要先定义再使用,比如你的day变量根本没有定义,直接就用了,应该是写错了,day2或者day3吧,宗旨都是变量名字的错。自己好好检查检查 这里给你改了一个自己看看是否符合你的需求吧
import java.util.Vector;
import com.ipackage.*;
/**
* @author Administrator
*
*/
import javax.swing.JOptionPane;
public class Month{
public static void main(String[] args){
String yearString = JOptionPane.showInputDialog("Enter a year");
int year = Integer.parseInt(yearString);
String firstDayString= JOptionPane.showInputDialog("Enter the first day is");
int firstDay= Integer.parseInt(firstDayString);
int day1=31;
int day2=30;
int day3=0;
int firsday1=0;
int firsday2=0;
int firsday3=0;
int firsday4=0;
int firsday5=0;
int firsday6=0;
int firsday7=0;
int firsday8=0;
int firsday9=0;
int firsday10=0;
int firsday11=0;
int firsday12=0;
String firstday11;
String firstday21;
String firstday31;
String firstday41;
String firstday51;
String firstday61;
String firstday71;
String firstday81;
String firstday91;
String firstday101;
String firstday111;
String firstday121;
{if((year%4==0&&year%100!=0)||(year%400==0)) day3 = 29;
else day3 = 28;}
firsday1=firstDay;
firstday11=String.valueOf(firsday1);
firsday2=(firstDay+day1)%7;
firstday21=String.valueOf(firsday2);
firsday3=(firstDay+day1+day3)%7;
firstday31=String.valueOf(firsday3);
firsday4=(firstDay+day1*2+day3)%7;
firstday41=String.valueOf(firsday4);
firsday5=(firstDay+day1*2+day3+day2)%7;
firstday51=String.valueOf(firsday5);
firsday6=(firstDay+day1*3+day3+day2)%7;
firstday61=String.valueOf(firsday6);
firsday7=(firstDay+day1*3+day3+day2*2)%7;
firstday71=String.valueOf(firsday7);
firsday8=(firstDay+day1*4+day3+day2*2)%7;
firstday81=String.valueOf(firsday8);
firsday9=(firstDay+day1*5+day3+day2*2)%7;
firstday91=String.valueOf(firsday9);
firsday10=(firstDay+day1*5+day3+day2*3)%7;
firstday101=String.valueOf(firsday10);
firsday11=(firstDay+day1*6+day3+day2*3)%7;
firstday111=String.valueOf(firstday11);
firsday12=(firstDay+day1*6+day3+day2*4)%7;
firstday121=String.valueOf(firsday12);
String output="January 1, " + year + " is "+firstday11+"Feburary 1, " + year + " is "+firstday21
+"March 1, " + year + " is "+firstday31+ "April 1, " + year + " is "+firstday41+"May 1, " + year + " is "
+firstday51+"June 1, " + year + " is "+firstday61+"July 1, " + year + " is "+firstday71+
"August 1, " + year + " is "+firstday81+"September 1, " + year + " is "+firstday91+"October 1, " + year + " is "
+firstday101+"November 1, " + year + " is "+firstday111+"December 1, " + year + " is "+firstday121;
}
}
F. 急:用java如何得到当前月的第一天是星期几
这个比较简单,4行代码就行
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat format = new SimpleDateFormat("E");
System.out.println("本月第一天是:" + format.format(calendar.getTime()));
如果你要得到一个数字的话就是:
calendar.get(Calendar.DAY_OF_WEEK),当然,这个结果是以星期天为第一天算出来的,如果要换成星期一开始,减1就行了
希望能帮助你。
G. java怎么得到某个月的第一天和最后一天的日期,以2012-06为例
public class DateUtil { /** * 获取给定时间所在周的第一天(Sunday)的日期和最后一天(Saturday)的日期 * * @param calendar * @return Date数组,[0]为第一天的日期,[1]最后一天的日期 */ public Date[] getWeekStartAndEndDate(Calendar calendar) { Date[] dates = new Date[2]; Date firstDateOfWeek, lastDateOfWeek; // 得到当天是这周的第几天 int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 减去dayOfWeek,得到第一天的日期,因为Calendar用0-6代表一周七天,所以要减一 calendar.add(Calendar.DAY_OF_WEEK, -(dayOfWeek - 1)); firstDateOfWeek = calendar.getTime(); // 每周7天,加6,得到最后一天的日子 calendar.add(Calendar.DAY_OF_WEEK, 6); lastDateOfWeek = calendar.getTime(); dates[0] = firstDateOfWeek; dates[1] = lastDateOfWeek; return dates; } /** * 获取给定时间所在月的第一天F的日期和最后一天的日期 * * @param calendar * @return Date数组,[0]为第一天的日期,[1]最后一天的日期 */ public Date[] getMonthStartAndEndDate(Calendar calendar) { Date[] dates = new Date[2]; Date firstDateOfMonth, lastDateOfMonth; // 得到当天是这月的第几天 int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // 减去dayOfMonth,得到第一天的日期,因为Calendar用0代表每月的第一天,所以要减一 calendar.add(Calendar.DAY_OF_MONTH, -(dayOfMonth - 1)); firstDateOfMonth = calendar.getTime(); // calendar.getActualMaximum(Calendar.DAY_OF_MONTH)得到这个月有几天 calendar.add(Calendar.DAY_OF_MONTH, calendar .getActualMaximum(Calendar.DAY_OF_MONTH) - 1); lastDateOfMonth = calendar.getTime(); dates[0] = firstDateOfMonth; dates[1] = lastDateOfMonth; return dates; } public static void main(String[] args) { DateUtil dateUtil = new DateUtil(); Calendar now = Calendar.getInstance(); Date[] weekDates = dateUtil.getWeekStartAndEndDate(now); Date[] monthDates = dateUtil.getMonthStartAndEndDate(now); System.out.println("firstDateOfWeek: " + weekDates[0] + " ,lastDateOfWeek: " + weekDates[1]); System.out.println("firstDateOfMonth: " + monthDates[0] + " ,lastDateOfMonth: " + monthDates[1]); }}
H. java 获取任意时间的月第一天
String.split("-");得到yyyy,mm,hh字符串
然后Integer.parseint(mm|hh|yyyy)获得年月日int
switch(mm)
case 1:
case3:
。。。
就知道了啊。二月判断闰年