導航:首頁 > 編程語言 > 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月第一天相關的資料

熱點內容
xshell連接linux命令 瀏覽:5
把多個文件夾的內容合並在一起 瀏覽:479
基於單片機的澆花系統設計ppt 瀏覽:683
卷積碼編解碼及糾錯性能驗證實驗 瀏覽:352
請在刪除驅動器之前暫停加密什麼意思 瀏覽:785
光催化pdf 瀏覽:98
java字元串包含某字元 瀏覽:526
ssm身份認證源碼 瀏覽:466
預排序遍歷樹演算法 瀏覽:671
加密裝置如何打開ping功能 瀏覽:478
python下載372 瀏覽:901
u盤子文件夾隱藏 瀏覽:296
本地誤刪svn文件夾 瀏覽:685
海康威視python通道名 瀏覽:241
如何用app覆蓋全部曲庫 瀏覽:602
變異布林源碼 瀏覽:686
表格加密設置列印區域 瀏覽:437
卡耐基pdf下載 瀏覽:924
現在最流行的單片機 瀏覽:89
機頂盒刷機源碼 瀏覽:986