java考试题目,求给答案
第十二届全运会将于2013年8月31日在辽宁举行,请用java的日期类可运行程序(类名Test),计算输出开幕式当天是星期几,最后使用字节输出流把获取的星期存储到“log...
第十二届全运会将于2013年8月31日在辽宁举行,请用java的日期类可运行程序(类名Test),计算输出开幕式当天是星期几,最后使用字节输出流把获取的星期存储到“log.txt”日志文件中,并输出“log.txt”文件的长度。
展开
展开全部
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
/**
* 计算某天为星期几
* @author hf
*
*/
public class WeekDay
{
/**
*
* @param y 年
* @param m 月
* @param d 日
* @return
*/
public static String suan(int y,int m,int d)
{
if(m==1) {m=13;y--;}
if(m==2) {m=14;y--;}
int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
String weekstr="";
switch(week)
{
case 0: weekstr="一"; break;
case 1: weekstr="二"; break;
case 2: weekstr="三"; break;
case 3: weekstr="四"; break;
case 4: weekstr="五"; break;
case 5: weekstr="六"; break;
case 6: weekstr="日"; break;
}
return weekstr;
}
public static void main(String[] args){
Scanner s=new Scanner(System.in);
System.out.println("请输入年月日");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
String s1=suan(a,b,c);
try {
File f = new File("E:\\123.txt");
if(f.exists()){
System.out.print("文件存在");
}else{
f.createNewFile();//不存在则创建
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
System.out.println("文件的长度为:"+new File("E:\\123.txt").length());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
/**
* 计算某天为星期几
* @author hf
*
*/
public class WeekDay
{
/**
*
* @param y 年
* @param m 月
* @param d 日
* @return
*/
public static String suan(int y,int m,int d)
{
if(m==1) {m=13;y--;}
if(m==2) {m=14;y--;}
int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
String weekstr="";
switch(week)
{
case 0: weekstr="一"; break;
case 1: weekstr="二"; break;
case 2: weekstr="三"; break;
case 3: weekstr="四"; break;
case 4: weekstr="五"; break;
case 5: weekstr="六"; break;
case 6: weekstr="日"; break;
}
return weekstr;
}
public static void main(String[] args){
Scanner s=new Scanner(System.in);
System.out.println("请输入年月日");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
String s1=suan(a,b,c);
try {
File f = new File("E:\\123.txt");
if(f.exists()){
System.out.print("文件存在");
}else{
f.createNewFile();//不存在则创建
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
System.out.println("文件的长度为:"+new File("E:\\123.txt").length());
} catch (Exception e) {
e.printStackTrace();
}
}
}
展开全部
public static void main(String[] args) {
Date date= new Date();
SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
try {
date= d.parse("2013-8-31"); //自己定义的时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
String mydate=simpleDateFormat.format(date);//
System.out.println(mydate);
FileOutputStream fos = new FileOutputStream("log.txt");//会保存在你当前工程下,与你的test.java同级目录
fos.write(mydate.getBytes("UTF-8"));
fos.close();
FileInputStream fr = new FileInputStream("log.txt");
byte[] buf = new byte[1024];
int len = fr.read(buf);
System.out.println(len);// 你要的长度
String myStr = new String(buf,0,len,"UTF-8");
System.out.println(myStr); // 重新读取log.txt内容并输出
} catch (Exception e) {
e.printStackTrace();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) throws ParseException, IOException {
// TODO Auto-generated method stub
String[] weekDays={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
//char[] weekDaysC = {'1','12'};
String date = "2013-08-31";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
int w =c.get(Calendar.DAY_OF_WEEK)-1;
if(w<0){
w=0;
}
out(weekDays[w])
}
public void out(String weekDays)throws ParseException, IOException{
File outFile= new File("log.txt");
try {
FileOutputStream out= new FileOutputStream(outFile);
OutputStreamWriter ow = new OutputStreamWriter(out);
ow.write(weekDays);
ow.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
String[] weekDays={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
//char[] weekDaysC = {'1','12'};
String date = "2013-08-31";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
int w =c.get(Calendar.DAY_OF_WEEK)-1;
if(w<0){
w=0;
}
out(weekDays[w])
}
public void out(String weekDays)throws ParseException, IOException{
File outFile= new File("log.txt");
try {
FileOutputStream out= new FileOutputStream(outFile);
OutputStreamWriter ow = new OutputStreamWriter(out);
ow.write(weekDays);
ow.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询