
public static void main(String[] agrs) {
String newtime="2013-7-21";
Systemoutprint(testDate(newtime));
}
public static String testDate(String newtime) {
String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
Calendar c = CalendargetInstance();// 获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
csetTime(sdfparse(newtime));
} catch (ParseException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return dayNames[cget(CalendarDAY_OF_WEEK)-1];
}
对于单双数日,能被2整除就是双数日,反之是单数日
下面是一个简单的 Java 程序示例,用于打印指定时间段内每个日期是星期几:
```java
import javatimeDayOfWeek;
import javatimeLocalDate;
public class BirthdayPrint {
public static void main(String[] args) {
int startYear = 2022; // 开始年份
int endYear = 2025; // 结束年份
for (int year = startYear; year <= endYear; year++) {
for (int month = 1; month <= 12; month++) {
for (int day = 1; day <= 31; day++) { // 最大值为 31,超出月份天数的部分会自动忽略
try {
LocalDate date = LocalDateof(year, month, day);
DayOfWeek dow = dategetDayOfWeek();
Systemoutprintln(date + " is " + dow);
} catch (Exception e) { // 如果日期非法,则会抛出异常,这里捕获并忽略即可
}
}
}
}
}
}
```
此程序使用 Java 8 中的 `LocalDate` 类和 `DayOfWeek` 枚举类型,循环遍历指定时间范围内的所有日期,并通过 `getDayOfWeek()` 方法获取每个日期对应的星期几。最后将所有结果输出到控制台。
需要注意的是,在本程序中,我们没有做任何错误处理,例如输入的起始和结束年份必须合法、超出日期范围的日期会被自动忽略等等情况需要根据实际需求进行适当处理。
public class Dateb
{
String S;
public String(int year,int month,int date)
{
GregorianCalendar gc=new GregorianCalendar();
final char[] kor_week={'日','一','二','三','四','五','六'};
gcset(year,month-1,date);
char week=kor_week[gcget(CalendarDAY_OF_WEEK)-1];
S=“这天是星期"+week;
return S;
}
}
继续追问:
只能用流程控制啊 不用方法
补充回答:
public class Dateb
{
public static void main(String[] args)
{
String S;
GregorianCalendar gc=new GregorianCalendar();
String input1=JOptionPaneshowInputDialog("请输入年份");
String input2=JOptionPaneshowInputDialog("请输入月份");
String input3=JOptionPaneshowInputDialog("请输入日期");
int year=IntegerparseInt(input1);
int month=IntegerparseInt(input2);
int date=IntegerparseInt(input3);
final char[] kor_week={'日','一','二','三','四','五','六'};
gcset(year,month-1,date);
char week=kor_week[gcget(CalendarDAY_OF_WEEK)-1];
S=“这天是星期"+week;
Systemoutprintln(S);
}
}
对中国人来说,普遍使用的是yyyy-MM-dd。Java代码// 实现给定某日期,判断是星期几public static String getWeekday(String date){//必须yyyy-MM-ddSimpleDateFormat sd = new SimpleDateFormat(yyyy-MM-dd);SimpleDateFormat sdw = new SimpleDateFormat(E);Date d = null;try {d = sdparse(date);} catch (ParseException e) {eprintStackTrace();}return sdwformat(d);}判断今天是星期几import javautilCalendar; import javautilDate;public class WeekDay {public WeekDay() {super();// TODO Auto-generated constructor stub}public String getWeekDayString(){String weekString = ;final String dayNames[] = {星期日,星期一,星期二,星期三,星期四,星期五,星期六}; Calendar calendar = CalendargetInstance();Date date = new Date();calendarsetTime(date); int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);weekString = dayNames[dayOfWeek - 1];return weekString;}/ @param args/public static void main(String[] args) {// TODO Auto-generated method stub
输入某年某月某日,判断这一天是这一年的第几天 程序分析:以3月5日为例,应该先把前两个月的加起来, 然后再加上5天即本年的第几天,特殊情况, 闰年且输入月份大于3时需考虑多加一天。
java是一门面向对象的编程语言。
java语言具有功能强大和简单易用两个特征,具有简单性、面向对象、分布式等特点,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
/
判断当前日期是星期几<br>
<br>
@param pTime 修要判断的时间<br>
@return dayForWeek 判断结果<br>
@Exception 发生异常<br>
/
public static int dayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = CalendargetInstance();
csetTime(formatparse(pTime));
int dayForWeek = 0;
if(cget(CalendarDAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = cget(CalendarDAY_OF_WEEK) - 1;
}
return dayForWeek;
}
没必要那么麻烦,即使不用Calendar也可以用Date啊,虽然已经不被鼓励使用了
Date date = new Date() ;
datesetYear(n) ;
datesetMonth(y+1) ;
datesetDate(t) ;
Systemoutprintln(dategetDay()+1) ;
e = dategetDay()+1 ;
这样直接就获取是星期几了,e就是星期几
如果你实在是想练习一下switch的话,main函数我改了,以下:
public static void main( String[] args )
{
int d , n , y , t , x , e , a ;
n = IntegerparseInt(JOptionPaneshowInputDialog("请输入年号")) ;
y = IntegerparseInt(JOptionPaneshowInputDialog("请输入月份")) ;
t = IntegerparseInt(JOptionPaneshowInputDialog("请输入几号")) ;
d = n + (n - 1) / 4 - (n - 1) / 100 + (n - 1) / 400 ;
if ( n % 4 == 0 )
a = 28 ;
else
a = 29 ;
switch (y)
{
case 1 :
x = t - 1 ;
break ;
case 2 :
x = t + 30 ;
break ;
case 3 :
x = a + t + 30 ;
break ;
case 4 :
x = a + t + 31 + 30 ;
break ;
case 5 :
x = a + t + 31 + 30 + 30 ;
break ;
case 6 :
x = a + t + 31 + 30 + 31 + 30 ;
break ;
case 7 :
x = a + t + 31 + 30 + 31 + 30 + 30 ;
break ;
case 8 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 ;
break ;
case 9 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 ;
break ;
case 10 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 + 30 ;
break ;
case 11 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 + 30 + 31 ;
break ;
case 12 :
x = a + t + 31 + 30 + 31 + 30 + 30 + 31 + 31 + 30 + 30 + 31 ;
break ;
default :
x = 0 ;
break ;
}
e = (d + x) % 7 ;
if ( e == 0 )
JOptionPaneshowMessageDialog(null, "这天是星期天") ;
if ( !(e == 0) )
JOptionPaneshowMessageDialog(null, "这天是星期" + e) ;
}
以上就是关于Java 日期如何判断是星期几求大神代码全部的内容,包括:Java 日期如何判断是星期几求大神代码、java编程打印大学四年的生日都是星期几、判断输入的年月日时星期几,用java实现等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)