java 语言 输入一个日期 显示出星期几

如题所述

写完了,测试没有问题。程序中有注释。可以看看,不懂的可以给我消息。
import javax.swing.JOptionPane;

public class DateTest {

/**
* @param args
*/
//上面是非闰年的,下面是闰年的每个月的天数
static int M[]={31,28,31,30,31,30,31,31,30,31,30,31};
static int M1[]={31,29,31,30,31,30,31,31,30,31,30,31};

static int year = 0;
static int month = 0;
static int day = 0;
static int sum_day = 0;

public static void main(String[] args) {
// TODO Auto-generated method stub
String date=JOptionPane.showInputDialog(null,"请输入日期YYYY-MM-DD:","是星期几?",JOptionPane.PLAIN_MESSAGE);

//2007-01-01是星期一,选择2007就是因为他的第一天是星期一。
//先计算输入的日期和2007年1月1日的差距天数

//将输入的信息的年月日信息提取出来
String arr[] = date.split("-");
year = Integer.parseInt(arr[0]);
month = Integer.parseInt(arr[1]);
day = Integer.parseInt(arr[2]);

//如果是2007年之后的
if(year>2007)
{
int i;
for(i=2007;i<year;i++)
{
if(check(i))
sum_day += 366;
else sum_day += 365;
}
comCurYear(year,0);
}
else if(year==2007)
{
comCurYear(year,0);
}
else//2007年之前的
{
int i;
for(i=2006;i>year;i--)
{
if(check(i))
sum_day += 366;
else sum_day += 365;
}
comCurYear(year,1);
}
//由于只有7个我就全部输出来了。没有用数组来判断。
int temp = sum_day%7;
if(temp==1)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期一");
if(temp==2)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期二");
if(temp==3)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期三");
if(temp==4)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期四");
if(temp==5)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期五");
if(temp==6)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期六");
if(temp==0)
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+day+"日是星期日");
}

//检验一年是不是闰年
public static boolean check(int Y)
{
if(Y%400==0) return true;
else if(Y%4==0&&Y%100!=0) return true;
else return false;
}
public static void comCurYear(int Y,int flag)
{
int year_flag=0;
if(check(Y)) year_flag=1;

int k;
if(flag==0)//year>=2007
{
for(k=1;k<=12;k++)
{
if(k<month)
{
if(year_flag==1) //闰年
{
sum_day += M1[k-1];
}
else sum_day += M[k-1];
}
else if(k==month) sum_day += day;
else if(k>month) break;
}
}
if(flag==1)//year<2007
{
for(k=12;k>=1;k--)
{
if(k>month)
{
if(year_flag==1) //闰年
{
sum_day += M1[k-1];
}
else sum_day += M[k-1];
}
else if(k==month) //闰年
{
if(year_flag==1)
{
sum_day += M1[k-1]-day;
}
else sum_day += M[k-1]-day;
}
else if(k<month) break;
}
//下面两句话是对2007之前的年做一个处理,比如相差5天,其实是应该不是星期5而是星期2
int temp = sum_day%7;
sum_day = sum_day-temp + 7-temp;
}
return ;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-14
我写给你吧,其实这些都不难得。

完整代码如下:
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JOptionPane;

public class DateUtil {

public static final String[] weekdays = new DateFormatSymbols().getWeekdays();

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

boolean isValidInput = false;

String dateReg = "^\\d{4}-[0-1]\\d-[0-3][0-9]";
String inputDate = null;

do{
inputDate = JOptionPane.showInputDialog("Please input a date(yyyy-MM-dd)");

isValidInput = inputDate.trim().matches(dateReg);

if(!isValidInput){
JOptionPane.showMessageDialog(null, "Invalid date format. Please check and input again!");
}
}while(!isValidInput);

Calendar cal = Calendar.getInstance();
cal.setTime((new SimpleDateFormat("yyyy-MM-dd")).parse(inputDate));

JOptionPane.showMessageDialog(null, inputDate + " is " + weekdays[cal.get(Calendar.DAY_OF_WEEK)]);
}
}
第2个回答  2011-01-14
public static int getDay_week(Date date){
int week=0;
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
week=calendar.get(Calendar.DAY_OF_WEEK);
return week-1;
}本回答被网友采纳
第3个回答  2011-01-15
=========================== 1 ==========================
import javax.swing.*;
import java.lang.*;

public class Wanyear
{
public static void main(String[] args)
{
String input=JOptionPane.showInputDialog
("Please Enter the year");

int year=Integer.parseInt(input);

String input1=JOptionPane.showInputDialog
("Please Enter the Month");

int m=Integer.parseInt(input1);

String input2=JOptionPane.showInputDialog
("Please Enter the Day");

int d=Integer.parseInt(input2);

String Nyear=Integer.toString(year);

String Sc=Nyear.substring(0,1);
String Sy=Nyear.substring(2,3);

int c=Integer.parseInt(Sc);
int y=Integer.parseInt(Sy);

if(m<3){
year--;
m+=12;}

else
c--;

int w=(c/4)-(2*c)+y+(y/4)+(13*(m+1)/5)+d-1;
int result=0;

if(w>=0)
result=w%7;
else
result=(w+700)%7;

switch(result)
{
case 0: System.out.println("SUN"); break;
case 1: System.out.println("MON"); break;
case 2: System.out.println("TUE"); break;
case 3: System.out.println("WED"); break;
case 4: System.out.println("THU"); break;
case 5: System.out.println("FRI"); break;
case 6: System.out.println("SAT"); break;
}

}
}
======================== 2 =====================
import java.io.*;
class putout{
public void putout(int f,int x,int y){
int i;
int a[]= new int[40];
System.out.println(" 日 一 二 三 四 五 六 "+" "+f+"月");
for (i=0;i<x;i++)

for(i=x;i<x+y;i++)
a[i]=i-x+1;
for(i=x;i<x+y;i++)
{
if (i%7==0)
System.out.print("\n");
if (a[i]<10)
System.out.print(" "+a[i]);
else
System.out.print(" "+a[i]);
}
System.out.println("\n");
}
}
class st{
public static void main(String args[])throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.println("请输入一个年份");
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 && year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;i<year;i++)
{if((i%4==0 && i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i<13;i++){
switch(i){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
}
}
}
}

给了你2个,不知道你要的是显示的还是求那一天的
不过现在能解决你的问题
相似回答