首页 新闻 会员 周边

新手java小问题

0
悬赏园豆:10 [已解决问题] 解决于 2012-03-07 16:58
import java.util.*;
public class Time{
public static void main(String[] args){
Date now = new Date();
int year =now.getYear() +1900;
int month =now.getMonth()+1;
int date =now.getDate();
String day="";
switch(now.getDay()+1){
case 0:
day="星期日";
break;
case 1:
day="星期一";
break;
case 2:
day="星期二";
break;
case 3:
day="星期三";
break;
case 4:
day="星期四";
break;
case 5:
day="星期五";
break;
case 6:
day="星期六";
break;
}
int hour = now.getHours();
int temp = now.getMinutes();
String min=temp<10?"0"+temp:""+temp;
temp = now.getSeconds();
String sec=temp<10?"0"+temp:""+temp;
System.out.println("现在的时间为:"+year +"年"+month+"月"+day+"日"+hour+"点"+min+"分"+min+"秒");

}
}
< >
分享
最佳答案
0

这个能运行,结果也正确,不过好多方法过时了:

package com.hero;

import java.util.*;

public class TimerTest {
public static void main(String[] args) {
Date now = new Date();
int year = now.getYear() + 1900;
int month = now.getMonth() + 1;
int date = now.getDate();
String day = "";
switch (now.getDay() + 1) {
case 0:
day = "星期日";
break;
case 1:
day = "星期一";
break;
case 2:
day = "星期二";
break;
case 3:
day = "星期三";
break;
case 4:
day = "星期四";
break;
case 5:
day = "星期五";
break;
case 6:
day = "星期六";
break;
}
int hour = now.getHours();
int temp = now.getMinutes();
String min = temp < 10 ? "0" + temp : "" + temp;
temp = now.getSeconds();
String sec = temp < 10 ? "0" + temp : "" + temp;
System.out.println("现在的时间为:" + year + "年" + month + "月" + date + "日"
+ hour + "点" + min + "分" + sec + "秒 " + day);
}
}

一下的方法,不会有任何警告:

package com.hero;

import java.util.Calendar;

public class TimerTest {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int date = now.get(Calendar.DAY_OF_MONTH);
String day = "";
switch (now.get(Calendar.DAY_OF_WEEK)) {
case 1:
day = "星期日";
break;
case 2:
day = "星期一";
break;
case 3:
day = "星期二";
break;
case 4:
day = "星期三";
break;
case 5:
day = "星期四";
break;
case 6:
day = "星期五";
break;
case 7:
day = "星期六";
break;
}
int hour = now.get(Calendar.HOUR);
int temp = now.get(Calendar.MINUTE);
String min = temp < 10 ? "0" + temp : "" + temp;
temp = now.get(Calendar.SECOND);
String sec = temp < 10 ? "0" + temp : "" + temp;
System.out.println("现在的时间为:" + year + "年" + month + "月" + date + "日"
+ hour + "点" + min + "分" + sec + "秒 " + day);
}
}



 

收获园豆:10
英雄小强 | 菜鸟二级 |园豆:292 | 2012-03-05 16:36
package wyf.jc;
class Testpack{
public static void main(String agrs[]){
System.out.println("我在包wyf.jc里呢,你给我装进去了");
}
}


问一个简单的包问题,我这个是什么问题啊

夜雨星辰- | 园豆:194 (初学一级) | 2012-03-07 15:32

夜雨星辰- | 园豆:194 (初学一级) | 2012-03-07 15:33

@Future曙光: 因为你的类不是public的,无法直接访问。

英雄小强 | 园豆:292 (菜鸟二级) | 2012-03-07 19:33
其他回答(3)
0

哪里错?编译不了?还是运行结果跟预期不一样?预期的结果是什么?实际的结果是什么?

水牛刀刀 | 园豆:6350 (大侠五级) | 2012-03-04 15:02

编译错误,不能执行,结果执行现在的时间

支持(0) 反对(0) 夜雨星辰- | 园豆:194 (初学一级) | 2012-03-04 15:04

@Future曙光: 编译报什么错。

支持(0) 反对(0) 水牛刀刀 | 园豆:6350 (大侠五级) | 2012-03-04 15:05

@水牛刀刀: 好多都是些 找不到符号 然后方法,类的错误

支持(0) 反对(0) 夜雨星辰- | 园豆:194 (初学一级) | 2012-03-04 22:17
0
public class TestCalendar {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar(Locale.CHINA);
        String[] weekDays = {"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String formate = String.format("现在的时间为:%d年%d月%d日%d点%d分%d秒%s",year,month,day,hour,minute,second,weekDays[dayOfWeek]) ;
        System.out.println(formate);
    }
}
SangBillLee | 园豆:212 (菜鸟二级) | 2012-05-29 15:41
0
把switch(now.getDay()+1)中的+1去掉就可以了。
java_firefly | 园豆:206 (菜鸟二级) | 2012-06-22 17:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册