導航:首頁 > 編程語言 > java獲取某月第一天

java獲取某月第一天

發布時間:2023-08-23 14:44:20

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:

。。。
就知道了啊。二月判斷閏年

閱讀全文

與java獲取某月第一天相關的資料

熱點內容
程序員厚黑 瀏覽:187
如何在閑魚淘二手安卓機 瀏覽:175
怎麼下載晨星app 瀏覽:132
兩台伺服器如何同步內容 瀏覽:808
伺服器共用一個ip有什麼壞處 瀏覽:461
go加密exe 瀏覽:606
pdf改分欄 瀏覽:123
python執行怎麼寫 瀏覽:766
遇見她app怎麼加好友 瀏覽:548
手機怎麼設置app強制提醒 瀏覽:77
怎樣不用海綿做解壓玩具 瀏覽:81
為什麼遠程伺服器復制不了文件 瀏覽:715
打開app閃退怎麼回事 瀏覽:752
bcrpt加密原理 瀏覽:401
女程序員寫的小說 瀏覽:774
華為路由器ip設置命令 瀏覽:552
如何打開軟體伺服器 瀏覽:756
單片機介面技術及應用 瀏覽:751
linux下執行腳本文件 瀏覽:127
撥號加密保護 瀏覽:436