程序如下:
import java.text.DateFormat;
import java.util.*;
public class JspCalendar {
Calendar calendar = null;
public JspCalendar() {
calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
}
public int getYear() {
return calendar.get(Calendar.YEAR);
}
public String getMonth() {
int m = getMonthInt();
String[] months = new String [] { "1", "2", "3",
"4", "5", "6",
"7", "8", "9",
"10", "11", "12" };
if (m > 12)
return "Unknown to Man";
return months[m - 1];
}
public String getDay() {
int x = getDayOfWeek();
String[] days = new String[] {"日","一", "二", "三",
"四", "五", "六"};
if (x > 7)
return getStr("Unknown to Man");
return getStr(days[x - 1]);
}
public int getMonthInt() {
return 1 + calendar.get(Calendar.MONTH);
}
public String getDate() {
return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear();
}
public String getTime() {
return getHour() + ":" + getMinute() + ":" + getSecond();
}
public int getDayOfMonth() {
return calendar.get(Calendar.DAY_OF_MONTH);
}
public int getDayOfYear() {
return calendar.get(Calendar.DAY_OF_YEAR);
}
public int getWeekOfYear() {
return calendar.get(Calendar.WEEK_OF_YEAR);
}
public int getWeekOfMonth() {
return calendar.get(Calendar.WEEK_OF_MONTH);
}
public int getDayOfWeek() {
return calendar.get(Calendar.DAY_OF_WEEK);
}
public int getHour() {
return calendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinute() {
return calendar.get(Calendar.MINUTE);
}
public int getSecond() {
return calendar.get(Calendar.SECOND);
}
public static void main(String args[]) {
JspCalendar db = new JspCalendar();
p("date: " + db.getDayOfMonth());
p("year: " + db.getYear());
p("month: " + db.getMonth());
p("time: " + db.getTime());
p("date: " + db.getDate());
p("Day: " + db.getDay());
p("DayOfYear: " + db.getDayOfYear());
p("WeekOfYear: " + db.getWeekOfYear());
p("era: " + db.getEra());
p("ampm: " + db.getAMPM());
p("DST: " + db.getDSTOffset());
p("ZONE Offset: " + db.getZoneOffset());
p("TIMEZONE: " + db.getUSTimeZone());
}
private static void p(String x) {
System.out.println(x);
}
public int getEra() {
return calendar.get(Calendar.ERA);
}
public String getUSTimeZone() {
String[] zones = new String[] {"Hawaii", "Alaskan", "Pacific",
"Mountain", "Central", "Eastern"};
return zones[10 - getZoneOffset()];
}
public int getZoneOffset() {
return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000);
}
public int getDSTOffset() {
return calendar.get(Calendar.DST_OFFSET)/(60*60*1000);
}
public int getAMPM() {
return calendar.get(Calendar.AM_PM);
}
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
}
return "null";
}
}
输出结果:
date: 2
year: 2006
month: 7
time: 14:3:57
date: 7/2/2006
Day: ?// 这里是?,而不是“日”,要怎么做才对呢?
DayOfYear: 183
WeekOfYear: 27
era: 1
ampm: 1
DST: 0
ZONE Offset: 8
TIMEZONE: Pacific
望大侠们赐教,不胜感激!!!
----------------解决方案--------------------------------------------------------
问题出在
public String getDay()
你再好好看看
----------------解决方案--------------------------------------------------------
public String getDay() 中直接return days[x-1]就好了……
----------------解决方案--------------------------------------------------------
但是,其实这是个JavaBean,我在一个JSP页面中调用它:<jsp:getProperty name="clock" property="day"/>,结果这个页面在IE中其它汉字都能正常显示,唯独这个通过JavaBean调用的星期几,显示的是:星期 ?(?应该是日的);大侠们知道是什么原因吗?
感激!!
----------------解决方案--------------------------------------------------------
你直接取出来就可以了,你把汉字转化成ISO-8859-1的byte数组后
此数组的长度只有1,也就是把两字节的汉字变成了一个字节的了,
这个时候当然是乱码
你直接取吧,这样肯定不是乱码,只要你把页面的字符集改成是:gb2312
----------------解决方案--------------------------------------------------------
我一开始就是直接取的,但是它显示的就是“?”;
页面的字符集就是gb2312,但我不知道为什么它就是显示?愁死人了!
[此贴子已经被作者于2006-7-2 17:42:46编辑过]
----------------解决方案--------------------------------------------------------
经常看到有些网站在首部都会显示年、月、日还有星期几,它们都是怎么实现的呀,难道不是通过JavaBean吗,为什么其它的都不是乱码,就唯独调用这个JavaBean就是乱码了呢???
大家如果要实现显示年、月、日、星期几 这个功能的话,会怎么做呢?
可否说一下,谢谢了!!!
[此贴子已经被作者于2006-7-2 18:19:37编辑过]
----------------解决方案--------------------------------------------------------
这里是J2SE 不是J2EE 你要问JSP的问题请到J2EE去发 谢谢
----------------解决方案--------------------------------------------------------
ok!
----------------解决方案--------------------------------------------------------