| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1146 人关注过本帖
标题:java如何设置时间差
只看楼主 加入收藏
cxf6672
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2006-4-18
收藏
 问题点数:0 回复次数:1 
java如何设置时间差

我下了一段java代码,但是代码里有一段设置了开始时间和终止时间,但其之间只相差一个月,我想要开始时间和终止时间相差半年(我要的是让他运行后(比如今天运行)则自动在终止时间是今天的日期2006年7月29日,而开始时间是半年前的时间即是2006年1月28日 ),但不知如何修改代码,请高手指定。代码运行后如图

图片附件: 游客没有浏览图片的权限,请 登录注册


其代码如下请高手指定我应如何修改代码才能使得开始时间和终止时间之间默认能相差半年或是一年(我要的是让他运行后(比如今天运行)则自动在终止时间是今天的日期2006年7月29日,而开始时间是半年前的时间即是2006年1月28日 )
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;
public class TimeDialog extends Dialog
implements WindowListener, MouseListener, ItemListener, ActionListener
{
private Button m_btOk;
private Button m_btClose;
private ModelCanvas m_oModelCanvas;
private Point m_oSize;
private Point m_oPosition;
private TextField m_oInputYearA;
private TextField m_oInputMonthA;
private TextField m_oInputDayA;
private TextField m_oInputYearB;
private TextField m_oInputMonthB;
private TextField m_oInputDayB;
private Choice m_chioceLeixing;
private Label m_lbTimeA;
private Label m_lbTimeB;
private Label m_lbYA;
private Label m_lbYB;
private Label m_lbMA;
private Label m_lbMB;
private Label m_lbDA;
private Label m_lbDB;
private Label m_lbTm;
private long m_oYearA;
private long m_oMonthA;
private long m_oDayA;
private long m_oYearB;
private long m_oMonthB;
private long m_oDayB;
private MachineManager m_oManager;
private Dialog m_dlgMsg;
private Label m_lbMessage;
private long m_lOkCount;
private int m_nTimeMode;
private Calendar m_timeA;
private Calendar m_timeB;

public int getYearA()
{
return (int)m_oYearA;
}

public int getMonthA()
{
return (int)m_oMonthA;
}

public void mouseClicked(MouseEvent mouseevent)
{
}

public void mousePressed(MouseEvent mouseevent)
{
}

private void showLoading(boolean flag)
{
}

public TimeDialog(ModelCanvas modelcanvas, MachineManager machinemanager, long l, long l1, long l2, long l3, long l4, long l5)
{
super((Frame)modelcanvas.getParent().getParent(), false);
m_btOk = new Button();
m_btClose = new Button();
m_oSize = new Point(240, 180);
m_oInputYearA = new TextField(4);
m_oInputMonthA = new TextField(2);
m_oInputDayA = new TextField(2);
m_oInputYearB = new TextField(4);
m_oInputMonthB = new TextField(4);
m_oInputDayB = new TextField(4);
m_chioceLeixing = new Choice();
m_lbTimeA = new Label();
m_lbTimeB = new Label();
m_lbYA = new Label();
m_lbYB = new Label();
m_lbMA = new Label();
m_lbMB = new Label();
m_lbDA = new Label();
m_lbDB = new Label();
m_lbTm = new Label();
m_oYearA = -1L;
m_oMonthA = -1L;
m_oDayA = -1L;
m_oYearB = -1L;
m_oMonthB = -1L;
m_oDayB = -1L;
m_dlgMsg = null;
m_lbMessage = null;
m_lOkCount = 0L;
m_nTimeMode = 0;
m_timeA = null;
m_timeB = null;
m_oModelCanvas = modelcanvas;
m_oYearA = l;
m_oMonthA = l1;
m_oDayA = l2;
m_oYearB = l3;
m_oMonthB = l4;
m_oDayB = l5;
m_oManager = machinemanager;
setTime(0);
Init(true);
}

public void windowDeactivated(WindowEvent windowevent)
{
}

public void mouseReleased(MouseEvent mouseevent)
{
}

public void itemStateChanged(ItemEvent itemevent)
{
int i = m_chioceLeixing.getSelectedIndex();
setTime(i);
setTimeText();
}

public void windowClosing(WindowEvent windowevent)
{
if(windowevent.getWindow() == this)
setVisible(false);
if(windowevent.getWindow() == m_dlgMsg)
{
m_dlgMsg.setVisible(false);
m_lOkCount = 1L;
}
}

public TimeDialog(ModelCanvas modelcanvas, MachineManager machinemanager)
{
super((Frame)modelcanvas.getParent().getParent(), false);
m_btOk = new Button();
m_btClose = new Button();
m_oSize = new Point(240, 180);
m_oInputYearA = new TextField(4);
m_oInputMonthA = new TextField(2);
m_oInputDayA = new TextField(2);
m_oInputYearB = new TextField(4);
m_oInputMonthB = new TextField(4);
m_oInputDayB = new TextField(4);
m_chioceLeixing = new Choice();
m_lbTimeA = new Label();
m_lbTimeB = new Label();
m_lbYA = new Label();
m_lbYB = new Label();
m_lbMA = new Label();
m_lbMB = new Label();
m_lbDA = new Label();
m_lbDB = new Label();
m_lbTm = new Label();
m_oYearA = -1L;
m_oMonthA = -1L;
m_oDayA = -1L;
m_oYearB = -1L;
m_oMonthB = -1L;
m_oDayB = -1L;
m_dlgMsg = null;
m_lbMessage = null;
m_lOkCount = 0L;
m_nTimeMode = 0;
m_timeA = null;
m_timeB = null;
m_oModelCanvas = modelcanvas;
m_oManager = machinemanager;
Init(true);
}

public void mouseMoved(MouseEvent mouseevent)
{
}

private void setTime(int i)
{
int j = 0;
int k = 0;
if(m_timeB == null)
{
m_timeB = Calendar.getInstance();
m_timeB.setTime(new Date());
j = m_timeB.get(5);
m_timeB.set(5, j);
}
j = m_timeB.get(5);
k = m_timeB.get(2);
m_oYearB = m_timeB.get(1);
m_oMonthB = m_timeB.get(2) + 1;
m_oDayB = m_timeB.get(5);
switch(i)
{
case 1: // '\001'
j -= 10;
break;

case 2: // '\002'
j -= 31;
break;

case 3: // '\003'
k -= 2;
break;

case 4: // '\004'
k -= 6;
break;

case 0: // '\0'
default:
j -= 5;
break;
}
if(m_timeA == null)
{
m_timeA = Calendar.getInstance();
m_timeA.setTime(new Date());
}
m_timeA.set(1, m_timeB.get(1));
m_timeA.set(2, k);
m_timeA.set(5, j);
m_oYearA = m_timeA.get(1);
m_oMonthA = m_timeA.get(2) + 1;
m_oDayA = m_timeA.get(5);
}

public int getDateB()
{
return (int)m_oDayB;
}

public void windowOpened(WindowEvent windowevent)
{
}

public void windowClosed(WindowEvent windowevent)
{
}

public void windowDeiconified(WindowEvent windowevent)
{
}

public void windowActivated(WindowEvent windowevent)
{
m_timeA.set(1, (int)m_oYearA);
m_timeA.set(2, (int)(m_oMonthA - 1L));
m_timeA.set(5, (int)m_oDayA);
m_timeB.set(1, (int)m_oYearB);
m_timeB.set(2, (int)(m_oMonthB - 1L));
m_timeB.set(5, (int)m_oDayB);
}

public void mouseEntered(MouseEvent mouseevent)
{
}

public void mouseExited(MouseEvent mouseevent)
{
}

[此贴子已经被作者于2006-7-29 16:36:49编辑过]

搜索更多相关主题的帖子: java 时间差 
2006-07-29 16:30
cxf6672
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2006-4-18
收藏
得分:0 

public void actionPerformed(ActionEvent actionevent)
{
if(actionevent.getActionCommand().compareTo("取消") == 0)
{
hide();
return;
}
if(actionevent.getActionCommand().compareTo("确定") == 0)
{
if(m_lOkCount == 1L)
{
m_lOkCount = 0L;
return;
}
if(m_oInputYearA.getText().length() > 0)
m_oYearA = Long.valueOf(m_oInputYearA.getText()).longValue();
else
m_oYearA = -1L;
if(m_oInputMonthA.getText().length() > 0)
m_oMonthA = Long.valueOf(m_oInputMonthA.getText()).longValue();
else
m_oMonthA = -1L;
if(m_oInputDayA.getText().length() > 0)
m_oDayA = Long.valueOf(m_oInputDayA.getText()).longValue();
else
m_oDayA = -1L;
if(m_oInputYearB.getText().length() > 0)
m_oYearB = Long.valueOf(m_oInputYearB.getText()).longValue();
else
m_oYearB = -1L;
if(m_oInputMonthB.getText().length() > 0)
m_oMonthB = Long.valueOf(m_oInputMonthB.getText()).longValue();
else
m_oMonthB = -1L;
if(m_oInputDayB.getText().length() > 0)
m_oDayB = Long.valueOf(m_oInputDayB.getText()).longValue();
else
m_oDayB = -1L;
if(m_oYearA > 0L)
{
if(m_oYearA < 1980L)
m_oYearA = 1980L;
if(m_oYearA > 2200L)
m_oYearA = 2200L;
if(m_oMonthA < 0L)
m_oMonthA = 1L;
if(m_oDayA < 0L)
m_oDayA = 1L;
Calendar calendar = Calendar.getInstance();
calendar.set(1, (int)m_oYearA);
calendar.set(2, (int)(m_oMonthA - 1L));
calendar.set(5, (int)m_oDayA);
m_oYearA = calendar.get(1);
m_oMonthA = calendar.get(2) + 1;
m_oDayA = calendar.get(5);
} else
{
m_oMonthA = -1L;
m_oDayA = -1L;
}
boolean flag = true;
if(m_oYearB > 0L)
{
if(m_oYearB < 1980L)
m_oYearB = 1980L;
if(m_oYearB > 2200L)
m_oYearB = 2200L;
if(m_oMonthB < 0L)
m_oMonthB = 1L;
if(m_oDayB < 0L)
m_oDayB = 1L;
Calendar calendar1 = Calendar.getInstance();
calendar1.set(1, (int)m_oYearB);
calendar1.set(2, (int)(m_oMonthB - 1L));
calendar1.set(5, (int)m_oDayB);
m_oYearB = calendar1.get(1);
m_oMonthB = calendar1.get(2) + 1;
m_oDayB = calendar1.get(5);
if(m_oYearA > 0L)
{
Calendar calendar2 = Calendar.getInstance();
calendar2.set(1, (int)m_oYearA);
calendar2.set(2, (int)(m_oMonthA - 1L));
calendar2.set(5, (int)m_oDayA);
if(calendar2.after(calendar1))
{
if(m_dlgMsg == null)
{
m_dlgMsg = new Dialog((Frame)m_oModelCanvas.getParent().getParent(), true);
m_dlgMsg.addWindowListener(this);
m_dlgMsg.setLayout(new GridLayout());
m_lbMessage = new Label();
m_dlgMsg.add("center", m_lbMessage);
m_lbMessage.setAlignment(1);
}
m_dlgMsg.setTitle("提示");
m_lbMessage.setText("开始时间必须小于终止时间。");
m_dlgMsg.setSize(170, 100);
m_dlgMsg.setBackground(new Color(212, 208, 200));
Dimension dimension = getSize();
Point point = getLocationOnScreen();
point.x += (dimension.width - 250) / 2;
point.y += (dimension.height - 150) / 2;
m_dlgMsg.setLocation(point);
m_dlgMsg.setVisible(true);
flag = false;
}
}
} else
{
m_oMonthB = -1L;
m_oDayB = -1L;
}
if(flag)
{
m_timeA.set(1, (int)m_oYearA);
m_timeA.set(2, (int)(m_oMonthA - 1L));
m_timeA.set(5, (int)m_oDayA);
m_timeB.set(1, (int)m_oYearB);
m_timeB.set(2, (int)(m_oMonthB - 1L));
m_timeB.set(5, (int)m_oDayB);
m_oManager.setTimer(m_oYearA, m_oMonthA, m_oDayA, m_oYearB, m_oMonthB, m_oDayB);
setTimeText();
hide();
m_oModelCanvas.m_bLoadLastPoints = true;
m_oModelCanvas.getImage();
m_oModelCanvas.m_bTimeChanged = true;
}
}
}

public int getYearB()
{
return (int)m_oYearB;
}

private void setTimeText()
{
if(m_oYearA > 0L)
{
m_oInputYearA.setText(Long.toString(m_oYearA));
m_oInputMonthA.setText(Long.toString(m_oMonthA));
m_oInputDayA.setText(Long.toString(m_oDayA));
}
if(m_oYearB > 0L)
{
m_oInputYearB.setText(Long.toString(m_oYearB));
m_oInputMonthB.setText(Long.toString(m_oMonthB));
m_oInputDayB.setText(Long.toString(m_oDayB));
}
}

public int getMonthB()
{
return (int)m_oMonthB;
}

private void Init(boolean flag)
{
addWindowListener(this);
addMouseListener(this);
Point point = m_oModelCanvas.getLocationOnScreen();
Dimension dimension = m_oModelCanvas.getSize();
setSize(m_oSize.x, m_oSize.y);
m_oPosition = new Point(point.x + (dimension.width - m_oSize.x) / 2, point.y + (dimension.height - m_oSize.y) / 2);
setLocation(m_oPosition);
setLayout(new BorderLayout());
setResizable(false);
setTitle("时间段设置");
add("", m_lbTimeA);
m_lbTimeA.setText("开始时间:");
m_lbTimeA.setBounds(10, 40, 70, 18);
add("", m_lbYA);
m_lbYA.setText("年");
m_lbYA.setBounds(120, 40, 20, 18);
add("", m_lbMA);
m_lbMA.setText("月");
m_lbMA.setBounds(160, 40, 20, 18);
add("", m_lbDA);
m_lbDA.setText("日");
m_lbDA.setBounds(200, 40, 20, 18);
add("", m_lbTimeB);
m_lbTimeB.setText("终止时间:");
m_lbTimeB.setBounds(10, 70, 70, 18);
add("", m_lbYB);
m_lbYB.setText("年");
m_lbYB.setBounds(120, 70, 20, 18);
add("", m_lbMB);
m_lbMB.setText("月");
m_lbMB.setBounds(160, 70, 20, 18);
add("", m_lbDB);
m_lbDB.setText("日");
m_lbDB.setBounds(200, 70, 20, 18);
add("", m_btOk);
m_btOk.addMouseListener(this);
m_btOk.setLabel("确定");
m_btOk.setBounds(40, 110, 60, 20);
add("", m_btClose);
m_btClose.addMouseListener(this);
m_btClose.setLabel("取消");
m_btClose.setBounds(140, 110, 60, 20);
add("", m_oInputYearA);
m_oInputYearA.setBounds(86, 40, 30, 18);
add("", m_oInputMonthA);
m_oInputMonthA.setBounds(140, 40, 20, 18);
add("", m_oInputDayA);
m_oInputDayA.setBounds(180, 40, 20, 18);
add("", m_oInputYearB);
m_oInputYearB.setBounds(86, 70, 30, 18);
add("", m_oInputMonthB);
m_oInputMonthB.setBounds(140, 70, 20, 18);
add("", m_oInputDayB);
m_oInputDayB.setBounds(180, 70, 20, 18);
add("", m_btOk);
m_btOk.addMouseListener(this);
m_btOk.setLabel("确定");
m_btOk.setBounds(40, 130, 60, 20);
m_btOk.addActionListener(this);
add("", m_btClose);
m_btClose.addMouseListener(this);
m_btClose.setLabel("取消");
m_btClose.setBounds(140, 130, 60, 20);
m_btClose.addActionListener(this);
add("", m_lbTm);
m_lbTm.setText("时段选择:");
m_lbTm.setBounds(10, 100, 60, 18);
add("", m_chioceLeixing);
m_chioceLeixing.addMouseListener(this);
m_chioceLeixing.setBounds(86, 100, 120, 20);
m_chioceLeixing.addItemListener(this);
if(flag)
{
m_chioceLeixing.addItem("前5天");
m_chioceLeixing.addItem("前10天");
m_chioceLeixing.addItem("前1月");
m_chioceLeixing.addItem("前2月");
m_chioceLeixing.addItem("前6月");
m_chioceLeixing.select(2);
}
setTime(2);
setTimeText();
}

public void windowIconified(WindowEvent windowevent)
{
}

public int getDateA()
{
return (int)m_oDayA;
}
}

2006-07-29 16:30
快速回复:java如何设置时间差
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060397 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved