导航
  • 主页
  • Java SE
  • J2ME
  • Web开发
  • Java EE
  • Eclipse
  • 问答

java根据日期计算星期几

maxliu911 2012-02-15 12:56:06
java根据日期计算星期几
...全文
2473 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
suihongri 2013-12-16
貌似有个基姆拉尔森公式哦!
回复
ilovefzq 2012-02-20
package ason;

import java.util.*;
public class TestDemo
{
public static void main(String[] args)
{

Calendar calendar = Calendar.getInstance();//获得一个日历

calendar.add(calendar.DAY_OF_WEEK, +0);
int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
String [] str = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
System.out.println(str[number]);
}
}
,这个代码还行啊,我测试了一下是好使的啊
回复
ljfolo59 2012-02-20
貌似是类似万年历的程序
回复
lavender975 2012-02-20
public static final String[] weeks = { "星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六", };
Calendar c = Calendar.getInstance();
int day=c.get(Calendar.DAY_OF_WEEK)-1;

int[]weekcount=new int[5];
for (int i = 0; i < weekcount.length; i++) {
if (day<=6) {
weekcount[i]=day;
day++;
}else{
day--;
day=6-day;
weekcount[i]=day;
day++;
}
}
回复
wwj13141988 2012-02-18

import java.util.Calendar;
import java.util.Date;

public class TestWeekDay
{
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();

int year = 201;
int month = 10;
int date = 16;
calendar.set(year,month-1,date);
int number = calendar.get(Calendar.DAY_OF_WEEK)-1;
//if(number < 0) number = 0 ;
String[] str = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
System.out.println(str[number]);
}
}

回复
hongkiton 2012-02-18
如果只是简单的使用 直接查API就行了 下面是自己写的根据日期求星期几类

//Calculating what date the day the user input, which start with 1st January ,1980(Tuesday).
// 2010-8-28


import java.util.*;
import java.io.*;
import java.text.*;

public class CalculateWeekDay {

private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));

private static PrintWriter stdOut =
new PrintWriter(System.out, true);

private static PrintWriter stdErr =
new PrintWriter(System.err, true);

public static void main(String[] args) throws Exception {

/**Date inputDate = new Date();

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); //格式化日期

System.out.println(f.format(inputDate));

String str = f.format(inputDate);*/

System.out.print("Please input the date: ");

stdErr.flush();

String str = stdIn.readLine();

String str1[] = str.split("-"); //分隔日期字符串

int year = Integer.parseInt(str1[0]);

int month = Integer.parseInt(str1[1]);

int day = Integer.parseInt(str1[2]);

int total,week,i;

boolean leap = false;

leap = (year%400 == 0) | (year%100 != 0) & (year%4 == 0); //判断当年是否是瑞年

week = 1; //起始日1979-12-31是monday

total = year - 1980 +(year - 1980 + 3)/4; //计算total的初值

//计算当年前几月的累计天数与total的初值之和

for(i = 1; i <= month - 1; i++) {

switch(i) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

total += 31;

break;

case 4:

case 6:

case 9:

case 11:

total += 30;

break;

case 2:

if(leap)
total += 29;

else
total += 28;

break;

}

}

total = total + day; //将本月天数加上

week = (week + total)%7;

System.out.print("today " + year + "-" + month + "-" +day + " is ");
stdErr.flush();

switch(week) {

case 0:
System.out.println("Sunday");
break;

case 1:
System.out.println("Monday");
break;

case 2:
System.out.println("Tuestay");
break;

case 3:
System.out.println("Wednesday");
break;

case 4:
System.out.println("Thursday");
break;

case 5:
System.out.println("Friday");
break;

case 6:
System.out.println("Saturday");
break;

}

}

}
回复
interceptor2012 2012-02-16
有bug

[Quote=引用 3 楼 monghuan 的回复:]
Java code

import java.util.*;
class TestDemo
{
public static void main(String[] args)
{
int year = 2012;
int month = 2;
int day = 15;
Calendar calendar =……
[/Quote]
回复
脉动 2012-02-16
什么情况呢,
回复
脉动 2012-02-15

import java.util.*;
class TestDemo
{
public static void main(String[] args)
{
int year = 2012;
int month = 2;
int day = 15;
Calendar calendar = Calendar.getInstance();//获得一个日历
calendar.set(year, month-1, day);//设置当前时间,月份是从0月开始计算
int number = calendar.get(Calendar.DAY_OF_WEEK);//星期表示1-7,是从星期日开始,
String [] str = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};
System.out.println(str[number]);
}
}
回复
安特矮油 2012-02-15
Date有个getDay()可以获取,但是这个方法已经不推荐使用了。不过应该还有别的方法可以获取的,你查查api吧
回复
香蕉猪 2012-02-15
给一个定位点,,,然后求之间的差数...
这样的解决方式网络上很多,,,楼主自己Google一下就好啦....
回复
发动态
发帖子
Java
创建于2007-09-28

4.6w+

社区成员

Java相关技术讨论
申请成为版主
社区公告
暂无公告