导航:首页 > 编程语言 > java月第一天

java月第一天

发布时间:2023-02-02 16:13:20

A. 急:用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就行了

希望能帮助你。

B. 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]); }}

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 获取任意时间的月第一天

String.split("-");得到yyyy,mm,hh字符串
然后Integer.parseint(mm|hh|yyyy)获得年月日int
switch(mm)
case 1:
case3:

。。。
就知道了啊。二月判断闰年

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中如何通过一个月份,获得该月份的第一天的日期

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(new Date());// 设置当前日期
int day = calendar.getMinimum(Calendar.DAY_OF_MONTH);// 取得当前月的最小日期(天)
calendar.set(Calendar.DAY_OF_MONTH, day);// 设置天
String sDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());// 格式化成字符串
System.out.println(sDate);// 打印
}
}

G. java 知道月份怎么获取第一天 最后一天

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class Test {

/**

* 获取某月的最后一天

* @Title:getLastDayOfMonth

* @Description:

* @param:@param year

* @param:@param month

* @param:@return

* @return:String

* @throws

*/

public static String getLastDayOfMonth(int year,int month) {

Calendar cal = Calendar.getInstance();

//设置年份

cal.set(Calendar.YEAR,year);

//设置月份

cal.set(Calendar.MONTH, month-1);

//获取某月最大天数

int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

//设置日历中月份的最大天数

cal.set(Calendar.DAY_OF_MONTH, lastDay);

//格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String lastDayOfMonth = sdf.format(cal.getTime());

return lastDayOfMonth;

}

/**

* 获取这个月的最后一天

* @Title:getLastDayOfCurrentMonth

* @Description:

* @param:@param year

* @param:@param month

* @param:@return

* @return:String

* @throws

*/

public static String getLastDayOfCurrentMonth() {

Calendar cal = Calendar.getInstance();

//获取某月最大天数

int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);

//设置日历中月份的最大天数

cal.set(Calendar.DAY_OF_MONTH, lastDay);

//格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String lastDayOfMonth = sdf.format(cal.getTime());

return lastDayOfMonth;

}

/**

* 获取某月的第一天

* @Title:getLastDayOfMonth

* @Description:

* @param:@param year

* @param:@param month

* @param:@return

* @return:String

* @throws

*/

public static String getFirstDayOfMonth(int year,int month) {

Calendar cal = Calendar.getInstance();

//设置年份

cal.set(Calendar.YEAR,year);

//设置月份

cal.set(Calendar.MONTH, month-1);

//获取某月最大天数

int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);

//设置日历中月份的最大天数

cal.set(Calendar.DAY_OF_MONTH, lastDay);

//格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String lastDayOfMonth = sdf.format(cal.getTime());

return lastDayOfMonth;

}

/**

* 获取这个月的第一天

* @Title:getFirstDayOfCurrentMonth

* @Description:

* @param:@param year

* @param:@param month

* @param:@return

* @return:String

* @throws

*/

public static String getFirstDayOfCurrentMonth() {

Calendar cal = Calendar.getInstance();

//获取某月最大天数

int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

//设置日历中月份的最大天数

cal.set(Calendar.DAY_OF_MONTH, lastDay);

//格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String lastDayOfMonth = sdf.format(cal.getTime());

return lastDayOfMonth;

}

/**

* @Title:main

* @Description:

* @param:@param args

* @return: void

* @throws

*/

public static void main(String[] args)

{

String lastDay = getLastDayOfMonth(2014,2);

System.out.println("获取2014年2月的最后一天:" + lastDay);

String currentLDay= getLastDayOfCurrentMonth();

System.out.println("获取本月的最后一天:" + currentLDay);

String firstDay = getFirstDayOfMonth(2014,2);

System.out.println("获取2014年2月的第一天:" + firstDay);

String currentFDay= getFirstDayOfCurrentMonth();

System.out.println("获取本月的第一天:" + currentFDay);

}

}

传上来的代码真难看,不知道怎么发文件,我给你截一张图

H. 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;
}

}

I. 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);

阅读全文

与java月第一天相关的资料

热点内容
addto新建文件夹什么意思 浏览:158
有服务器地址怎么安装软件 浏览:659
安卓如何完全清除数据 浏览:690
安卓安卓证书怎么信任 浏览:53
服务器被攻击如何解决 浏览:221
学霸变成程序员 浏览:881
c语言编译错误fatalerror 浏览:441
ipv4内部服务器地址怎么分配 浏览:463
java线程安全的方法 浏览:950
重复命令画梯形 浏览:164
在疫情就是命令 浏览:328
自己搭建一个什么服务器好玩 浏览:253
java基础马士兵 浏览:823
完美世界手游如何查看服务器 浏览:859
光遇安卓与ios什么时候互通 浏览:598
js如何运行时编译 浏览:917
引力app在哪里下载 浏览:609
编写app如何得到钱 浏览:801
吉利汽车软件放哪个文件夹安装 浏览:223
多文件编译c 浏览:543