当前位置:编程学习 > JAVA >>

swing当前时间问题

问下各位高手。我写的这个小程序。为什么显示的时间跟我电脑上的时间不一样呢。时间差很多。。求解答。谢谢各。。。。。


package hoemwork05;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;

public class TestHome05 {

public static void main(String args[]) throws IOException {

Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = df.format(date);
System.out.println(df.format(new Date()));

JFrame jframe = new JFrame();
jframe.setTitle(str);
jframe.setBounds(100, 100, 200, 200);
jframe.setVisible(true);
}
}

--------------------编程问答-------------------- 时区问题 --------------------编程问答-------------------- 由于你发错版了,所以我不会 --------------------编程问答-------------------- Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = df.format(date);
System.out.println(df.format(new Date()));

这段没什么问题啊 取的就是电脑时间 --------------------编程问答-------------------- 在我电脑上面没什么问题啊??不知道楼主有什么问题 --------------------编程问答-------------------- 哦,我好像在哪里见过你,你是做一个电子手表吧,没理由时间不对啊 --------------------编程问答-------------------- 你现实的是什么时间,说出来大家给你看看是不是时区问题。 --------------------编程问答--------------------
引用 1 楼 foolishdault 的回复:
时区问题

很显然,以前也碰到过。
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
TimeZone.setDefault(tz);   --------------------编程问答-------------------- System.setProperty("user.timezone", "Asia/Shanghai"); //也可以 --------------------编程问答--------------------
根据时区获取时间的方法
   // 获得的formatter
        DateFormat formatter = DateFormat.getDateTimeInstance();
        TimeZone timezone = TimeZone.getTimeZone("Asia/Tokyo");
//设置DateFormat的时区 

   formatter.setTimeZone(timezone);
        // 获得格式化后的时间
        String dateTime = formatter.format(new Date());
        System.out.println(dateTime);
 
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
        System.out.println(calendar.get(calendar.YEAR));
        // calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
        // calendar.get(calendar.HOUR_OF_DAY), calendar.get(calendar.MINUTE));
        Date date = new Date(calendar.get(Calendar.YEAR) - 1900, calendar.get(Calendar.MONTH),
                             calendar.get(Calendar.DAY_OF_MONTH), calendar.get(calendar.HOUR_OF_DAY),
                             calendar.get(calendar.MINUTE));

--------------------编程问答-------------------- 恩恩。。你好
引用 5 楼 buzaibeishang4617016 的回复:
哦,我好像在哪里见过你,你是做一个电子手表吧,没理由时间不对啊
--------------------编程问答-------------------- public class DateTest extends JFrame {
   public static final String YYYMMDD="yyyy-MM-dd HH:mm:ss";
   
   Thread time;
   JTextField t1,t2,t3;

public static void main(String[] args) {
DateTest r=new DateTest();
}
//构造
public DateTest(){
JPanel p=new JPanel();
t1=new JTextField(2);
t2=new JTextField(2);
t3=new JTextField(2);
JLabel timelbl=new JLabel("显示当前实列");
JLabel nowtimelbl=new JLabel("当前时间");
p.add(nowtimelbl);
Date newdate=new Date();

/*格式化时间*/
   SimpleDateFormat sfl=new SimpleDateFormat(YYYMMDD);
/*将格式化后年月日放入到表示时间的标签*/
   JLabel ymdLbl=new JLabel(sfl.format(newdate));
   p.add(ymdLbl);
   this.setTitle("钟哥的杰作");
   this.add("North",timelbl);
   this.add("Center",p);
   this.setBounds(400,200,500,300);
   this.setVisible(true);
} --------------------编程问答-------------------- 我试都好着呢啊,那个就是取得本机时间啊。你试着把自己电脑的时间改改,看是差多少,之后就可以判断时间到底怎么回事了。

delphi的Date日期和java的Date日期就差8个小时。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,