在JAVA中如何用数组实现把日期转换为星期几2
发布网友
发布时间:2023-09-24 20:14
我来回答
共5个回答
热心网友
时间:2024-12-13 10:05
这个问题不是很难,有关判断日期是否合法,其实已经有现成的包可以用,没有必要自己写方法来实现,代码如下:
import java.util.*;
public class WeekTest {
private String[][] javadays=new String[12][31];
public WeekTest(){
String[] weeks=new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int i=0;i<12;i++){
for(int j=0;j<31;j++){
GregorianCalendar gc=new GregorianCalendar();
gc.setLenient(false);
try{
gc.set(2008,i,j+1);
int week=gc.get(Calendar.DAY_OF_WEEK);
javadays[i][j]=new String(weeks[week-1]);
}catch(Exception e){}
}
}
}
public void randomPrint(){
HashSet<Integer> hs=new HashSet<Integer>();
while(true){
if(hs.size()==10)
break;
int day=((int)(Math.random()*1000))%366;
if(hs.contains(new Integer(day)))
continue;
hs.add(new Integer(day));
}
for(Integer day:hs){
GregorianCalendar gc=new GregorianCalendar(2008,0,1);
gc.add(Calendar.DATE,day.intValue());
int month=gc.get(Calendar.MONTH)+1;
int date=gc.get(Calendar.DAY_OF_MONTH);
System.out.println("2008年"+(month<10?"0"+month:month)+"月"+(date<10?"0"+date:date)+"日\t"+javadays[month-1][date-1]);
}
}
public void replaceAll(){
String[][] weekStr=new String[2][];
weekStr[0]=new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
weekStr[1]=new String[]{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
for(int i=0;i<12;i++){
for(int j=0;j<31;j++){
for(int k=0;k<7;k++){
if(javadays[i][j]!=null && javadays[i][j].equals(weekStr[0][k]))
javadays[i][j]=new String(weekStr[1][k]);
}
}
}
}
public static void main(String[] args) {
WeekTest wt=new WeekTest();
wt.randomPrint();
wt.replaceAll();
wt.randomPrint();
}
}
热心网友
时间:2024-12-13 10:05
import java.util.*;
public class WeekDay {
Calendar date = Calendar.getInstance();
private int getMaxDate(int moth){
moth = moth -1;
if(moth > 12 || moth < 0){
System.out.println("输入月份错误");
}
int maxDate = 0;
date.set(Calendar.MONTH, moth);
date.set(Calendar.DATE, 1);
date.roll(Calendar.DATE, -1);
maxDate = date.get(Calendar.DATE);
return maxDate;
}
private String getDay(int d){
if(d < 1 || d > 31){
return "输入日期错误";
}
date.set(Calendar.DATE, d);
int i = date.get(Calendar.DAY_OF_WEEK_IN_MONTH);
switch(i){
case 1:return "星期一";
case 2:return "星期二";
case 3:return "星期三";
case 4:return "星期四";
case 5:return "星期五";
case 6:return "星期六";
case 0:return "星期七";
}
return null;
}
private String[][] getJavaDays(){
String [][] javaDays= new String[12][31];
for(int i=0;i<12;i++){
for(int j=1;j<getMaxDate(i+1);j++){
javaDays[i][j]=getDay(j);
}
}
return javaDays;
}
private String replaceAll(String str){
if(str.equals("星期一")){
return "Monday";
}else if(str.equals("星期二")){
return "Tuesday ";
}
else if(str.equals("星期三")){
return "Wednesday";
}
else if(str.equals("星期四")){
return "Thursday";
}
else if(str.equals("星期五")){
return "Friday";
}
else if(str.equals("星期六")){
return "Saturday ";
}
else if(str.equals("星期七")){
return "Sunday";
}
return "输入错误";
}
public String getDayNow(int i,int j){
if(i < 0 || j< 1 || i > 12){
return "输入月份或者日期错误";
}else if(j > getMaxDate(i)){
return "输入日期错误";
}
String [][] javaDays=getJavaDays();
return javaDays[i][j];
}
public static void main(String args[]){
WeekDay wd = new WeekDay();
System.out.println("当前星期:"+wd.replaceAll(wd.getDayNow(7, 24)));
}
}
热心网友
时间:2024-12-13 10:06
public class JavaDay {
private static final byte MONDAY = 1;
private static final byte TUESDAY = 2;
private static final byte WEDNESDAY = 3;
private static final byte THURSDAY = 4;
private static final byte FRIDAY = 5;
private static final byte SATURDAY = 6;
private static final byte SUNDAY = 7;
private static final int YEAR = 2008;
private String[][] javadays = new String[12][];
/**
* 计算星期
*/
public void calculateWeek() {
for (int i = 0; i < javadays.length; i++) {
if (i == 1) {
javadays[1] = new String[29];
} else if (i <= 6 && i % 2 == 0 || i >= 7 && i % 2 == 1)
javadays[i] = new String[31];
else
javadays[i] = new String[30];
}
for (int i = 0; i < javadays.length; i++) {
String[] weeks = javadays[i];
for (int j = 0; j < weeks.length; j++)
weeks[j] = getWeek(YEAR, i, j);
weeks = null;
}
}
/**
* 利用公式W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7计算几月几号是星期几
*/
private String getWeek(int year, int month, int day) {
month++;
day++;
if (month <= 2) {
month += 12;
year--;
}
int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4
- year / 100 + year / 400) % 7;
week++;
String weekString = null;
switch (week) {
case MONDAY:
weekString = "星期一";
break;
case TUESDAY:
weekString = "星期二";
break;
case WEDNESDAY:
weekString = "星期三";
break;
case THURSDAY:
weekString = "星期四";
break;
case FRIDAY:
weekString = "星期五";
break;
case SATURDAY:
weekString = "星期六";
break;
case SUNDAY:
weekString = "星期日";
break;
}
return weekString;
}
public void replaceAll() {
for (int i = 0; i < javadays.length; i++) {
String[] weeks = javadays[i];
for (int j = 0; j < weeks.length; j++) {
if (weeks[j].equals("星期一"))
weeks[j] = "Monday";
else if (weeks[j].equals("星期二"))
weeks[j] = "Tuesday";
else if (weeks[j].equals("星期三"))
weeks[j] = "Wednesday";
else if (weeks[j].equals("星期四"))
weeks[j] = "Thursday";
else if (weeks[j].equals("星期五"))
weeks[j] = "Friday";
else if (weeks[j].equals("星期六"))
weeks[j] = "Saturday";
else if (weeks[j].equals("星期日"))
weeks[j] = "Sunday";
}
weeks = null;
}
}
public void printWeeks() {
for (int i = 0; i < javadays.length; i++) {
String[] weeks = javadays[i];
System.out.println((i + 1) + "月份:");
for (int j = 0; j < weeks.length; j++)
System.out.print(weeks[j] + "\t");
System.out.println();
weeks = null;
}
}
}
public class Test {
public static void main(String[] args) {
JavaDay javaday = new JavaDay();
javaday.calculateWeek();
javaday.printWeeks();
javaday.replaceAll();
javaday.printWeeks();
}
}
热心网友
时间:2024-12-13 10:06
是不是 你们明天考试的题呀!
你这个日期只是2008年的是吧
热心网友
时间:2024-12-13 10:07
稍等 这就给你写去