问下各位高手。我写的这个小程序。为什么显示的时间跟我电脑上的时间不一样呢。时间差很多。。求解答。谢谢各。。
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);
}
}

解决方案 »

  1.   

    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()));这段没什么问题啊 取的就是电脑时间
      

  2.   

    很显然,以前也碰到过。
    TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
    TimeZone.setDefault(tz);  
      

  3.   

    System.setProperty("user.timezone", "Asia/Shanghai"); //也可以
      

  4.   

    根据时区获取时间的方法
       // 获得的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.   

    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);
    }
      

  6.   

    我试都好着呢啊,那个就是取得本机时间啊。你试着把自己电脑的时间改改,看是差多少,之后就可以判断时间到底怎么回事了。delphi的Date日期和java的Date日期就差8个小时。