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+"秒");
}
}
这个能运行,结果也正确,不过好多方法过时了:
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);
}
}
package wyf.jc;
class Testpack{
public static void main(String agrs[]){
System.out.println("我在包wyf.jc里呢,你给我装进去了");
}
}
问一个简单的包问题,我这个是什么问题啊
@Future曙光: 因为你的类不是public的,无法直接访问。
哪里错?编译不了?还是运行结果跟预期不一样?预期的结果是什么?实际的结果是什么?
编译错误,不能执行,结果执行现在的时间
@Future曙光: 编译报什么错。
@水牛刀刀: 好多都是些 找不到符号 然后方法,类的错误
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); } }
把switch(now.getDay()+1)中的+1去掉就可以了。