在ABAP开发中如何获取选择日期前一个月的固定日期

 我来答
072jncgnt
2011-06-30 · TA获得超过2310个赞
知道大有可为答主
回答量:4534
采纳率:0%
帮助的人:862万
展开全部
没有实现的类,可以自己写一个。
这有一个类。你可以试一下:
package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.DocumentListener;
import javax.swing.text.DateFormatter;

import com.shiji.pastoral.common.arithmetic.CalculateDate;
import com.shiji.pastoral.common.arithmetic.GlobalStatus;
import com.shiji.pastoral.server.common.method.DebugPrint;

/**
* 日期控件
*
* @author 2008-5-23 上午11:38:14
*/
public class DatePanel extends JPanel {

private CalculateDate cal = new CalculateDate();

public JFormattedTextField hotelFormattedTextField;

public JButton dateButton;

private Window dialog;

private String labelName; // 每个Date的名称都不一样

private int x; // 跳出日期控件的x轴

private int y; // 跳出日期控件的y轴

public static String initDate = "1900-01-01";// CalculateDate.dateToString(CalculateDate.NULLDATE);

// // 系统默认Date

private boolean judge;

private String type = "-";

private static String time;

private JRadioButton enableRadioButton;

private boolean allowEmptyStr = true;// 是否允许文本框为空

public FocusAdapter f;

/**
* 系统初始化构造函数
*
* @author 2008-7-22 上午11:38:27
*
*/
public DatePanel() {
this(false);
}

public DatePanel(JDialog dialog) {
this(dialog, true);
}

public DatePanel(boolean showRadioBox) {
super();
dialog = DFrame.frame;
initGUI();
enableRadioButton.setVisible(showRadioBox);
}

/**
* 旧的方法,不建议使用,最终还是要删除的。。。by Jimmy
*
* @deprecated
* @param dialog
* @param labelName
* @param judge
*/
public DatePanel(Window dialog, String labelName, boolean judge,
boolean showRadioBox) {
super();
this.dialog = dialog;
this.labelName = labelName;
this.judge = judge;
initGUI();
enableRadioButton.setVisible(showRadioBox);
}

public DatePanel(Window dialog, String labelName, boolean judge) {
this(dialog, labelName, judge, false);
}

public DatePanel(Window dialog, boolean judge) {
super();
this.dialog = dialog;
this.labelName = "";
this.judge = judge;
initGUI();
enableRadioButton.setVisible(false);
}

private void initGUI() {

this.setPreferredSize(new java.awt.Dimension(139, 21));
BorderLayout thisLayout = new BorderLayout();
this.setLayout(thisLayout);
this.add(getDateButton(), BorderLayout.EAST);
this.add(getHotelFormattedTextField(), BorderLayout.CENTER);
this.add(getEnableRadioButton(), BorderLayout.WEST);

}

private JFormattedTextField getHotelFormattedTextField() {
// MaskFormatter mf = null;
// try {
// mf = new MaskFormatter("####-##-##");
// // mf.setValueContainsLiteralCharacters(false);
// } catch (Exception e) {
// e.printStackTrace();
// }

/*
* hotelFormattedTextField = new JFormattedTextField();
* hotelFormattedTextField.setDocument(new DateLimited(
* hotelFormattedTextField, new SimpleDateFormat("yyyy" + type + "MM" +
* type + "dd"))); hotelFormattedTextField.setText(initDate);
* hotelFormattedTextField.addFocusListener(new FocusAdapter() { public
* void focusLost(FocusEvent evt) { if
* (hotelFormattedTextField.getText().indexOf(" ") != -1 && judge) {
* JOptionPane.showMessageDialog(dialog, "Date is null!");
* hotelFormattedTextField.setText(" " + type + " " + type + " ");
* hotelFormattedTextField.requestFocus(); } } });
*/
// if(dateFormatType.trim().isEmpty()||dateFormatType==null)
// dateFormatType = CalculateDate.dateFormatType;// "yyyy-MM-dd";
DateFormat dateFormat = new SimpleDateFormat(
CalculateDate.dateFormatType);
DateFormatter df = new DateFormatter(dateFormat);
hotelFormattedTextField = new JFormattedTextField(df);
hotelFormattedTextField
.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
hotelFormattedTextField.setBorder(new SoftBevelBorder(
BevelBorder.LOWERED, null, null, null, null));
hotelFormattedTextField.setFont(new java.awt.Font(null, 1, 12));
hotelFormattedTextField.setValue(CalculateDate.HotelDate);
if (allowEmptyStr)
hotelFormattedTextField.setText("");// 设置为空时间
else
hotelFormattedTextField.setValue(CalculateDate.HotelDate);
hotelFormattedTextField.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent evt) {
if (hotelFormattedTextField.getText().trim().isEmpty()
&& allowEmptyStr) {
// DebugPrint.onePrint("现在输入的是空串");
hotelFormattedTextField
.setFocusLostBehavior(JFormattedTextField.PERSIST);// 不执行任何操作,不获取新的
// AbstractFormatter
// 也不更新该值。
} else {
hotelFormattedTextField
.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);// 恢复显示以匹配
// getValue,这可能丢失当前的编辑内容
if (hotelFormattedTextField.isEditValid()) {
// DebugPrint.onePrint("现在输入的是有效值");
hotelFormattedTextField.setFocusLostBehavior(JFormattedTextField.PERSIST);
if (CalculateDate.isNullDate(getDate())) {
// DebugPrint.onePrint("现在输入的时间是空时间");
hotelFormattedTextField.setText("");
}else if(getDate().compareTo(CalculateDate.NULLDATE)<0){
DefinedOptionPane.showMessageDialog(null,GlobalStatus.lang("Invalid Date can't less than 1900-01-01"), 1);
setDate(CalculateDate.getHotelDate(GlobalStatus.userStructure.getHotelID()));
}else if(getDate().compareTo(CalculateDate.MAXDATE)>0){
DefinedOptionPane.showMessageDialog(null,GlobalStatus.lang("Invalid Date can't more than 3000-12-31"), 1);
setDate(CalculateDate.getHotelDate(GlobalStatus.userStructure.getHotelID()));
}
} else {
if (!hotelFormattedTextField.getText().trim().isEmpty()) {
// DebugPrint.onePrint("现在输入的是无法效值");
DefinedOptionPane
.showMessageDialog(null,
GlobalStatus.lang("Invalid Date: ")
+ hotelFormattedTextField
.getText(), 1);
// hotelFormattedTextField.setText(initDate);
}
}

}
}
});

return hotelFormattedTextField;
}

private int getX(MouseEvent evt) {
int x1 = evt.getXOnScreen() - evt.getX()
- hotelFormattedTextField.getWidth();
int x2 = DFrame.width - CalendarDialog.width;
DebugPrint.jimmyPrintMsg(x1, x2);
return x1 > 0 ? Math.min(x1, x2) : 0;
}

private int getY(MouseEvent evt) {
int y1 = evt.getYOnScreen() + dateButton.getHeight() - evt.getY();
int y2 = DFrame.height - CalendarDialog.height;
if (y1 > y2)
y1 = evt.getYOnScreen() - evt.getY() - CalendarDialog.height;
return y1;
}

private JButton getDateButton() {
if (dateButton == null) {
dateButton = new JButton();
dateButton.setIcon(new ImageIcon(getClass().getClassLoader()
.getResource("img/down.png")));
dateButton.setPreferredSize(new java.awt.Dimension(20, 21));
dateButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
x = getX(evt);
y = getY(evt);
}
});
dateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
time = CalculateDate.dateToString(new CalendarDialog(
dialog, x, y, getDate()).getDate());
} catch (RuntimeException e) {
time = CalculateDate.dateToString(new CalendarDialog(
dialog, x, y, new Date()).getDate());
}
hotelFormattedTextField.setText(time.length() == 10
? time
: initDate);
hotelFormattedTextField.requestFocus();
time = initDate;
}
});
}
return dateButton;
}

/**
* 设置日期..不推荐用这个。。没测试过one
*
* @author 2008-8-5 上午10:37:33
*
* @param date
*/
public void setDate(String date) {
// setDate(new CalculateDate().stringToDate(date));
// hotelFormattedTextField.setValue(CalculateDate.dateFormat(new
// CalculateDate().stringToDate(date), dateFormatType));
if (date.trim().isEmpty())
hotelFormattedTextField.setText("");
else
setDate(CalculateDate.stringToDateExp(date));
}

public String getText() {
return CalculateDate.dateToString(getDate());
}

/**
* 获取日期
*
* @author 2008-8-5 上午10:37:46
*
* @return
*/
public Date getDate() {
// Date returndate;
// returndate = new CalculateDate().stringToDate(hotelFormattedTextField
// .getText());
if (hotelFormattedTextField.getText().trim().isEmpty()) {
// DebugPrint.onePrint("现在的日期是" + cal.stringToDate("1900-01-01"));
return cal.stringToDate("1900-01-01");
} else {
// DebugPrint.onePrint("现在的日期是"+
// cal.stringToDate(hotelFormattedTextField.getText()));
return cal.stringToDate(hotelFormattedTextField.getText());
}
}

/**
*
*
* @author Administrator 2008-9-16 下午02:33:24
*
* @param date
*/
public void setDate(Date date) {
// hotelFormattedTextField.setText(date != null ? new CalculateDate()
// .dateToString(date) : initDate);
if (CalculateDate.isNullDate(date))
hotelFormattedTextField.setText("");
else
hotelFormattedTextField.setText(CalculateDate.dateToString(date));
}

/**
* 设置文本框是否能为空
*
* @author One 2009-6-4上午10:09:14
*
* @param allow
*/
public void setAllowEmptyStr(boolean allow) {
allowEmptyStr = allow;
}

/**
* 设置错误日期的颜色
*
* @author One 2009-6-10下午04:27:58
*
* @param error
*/
public void setColour(boolean error) {
if (error)
hotelFormattedTextField.setForeground(Color.RED);
else
hotelFormattedTextField.setForeground(Color.BLACK);

}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
hotelFormattedTextField.setEnabled(enabled);
dateButton.setEnabled(enabled);
enableRadioButton.setSelected(enabled);
}

private JRadioButton getEnableRadioButton() {
if (enableRadioButton == null) {
enableRadioButton = new JRadioButton();
enableRadioButton.setSelected(true);
enableRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setEnabled(enableRadioButton.isSelected());
}
});
}
return enableRadioButton;
}

public static void main(String[] args) {
JDialog frame = new JDialog();
frame.getContentPane().add(new DatePanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

/**
* @author rainbow 2009-7-14 下午12:01:55
* @param listener
*
*/
public void addDocumentListener(DocumentListener listener) {
hotelFormattedTextField.getDocument().addDocumentListener(listener);
}

/**
* @author rainbow 2009-7-14 下午12:01:58
* @param listener
*
*/
public void removeDocumentListener(DocumentListener listener) {
hotelFormattedTextField.getDocument().removeDocumentListener(listener);
}
}
bryant_fox
2011-07-07 · 超过23用户采纳过TA的回答
知道答主
回答量:72
采纳率:0%
帮助的人:46万
展开全部
CALL FUNCTION 'MONTH_PLUS_DETERMINE'
EXPORTING
MONTHS = '-1'
OLDDATE = old_date
IMPORTING
NEWDATE = new_date
把选择的日期赋给old_date,new_date就是前一个月的日期

二楼在干嘛?楼主问的是ABAP,你拿一堆Java程序来干啥?
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tlymail
2011-06-29 · 超过11用户采纳过TA的回答
知道答主
回答量:28
采纳率:0%
帮助的人:23.8万
展开全部

1.    call function 'CCM_GO_BACK_MONTHS'

          exporting

            currdate   = date

            backmonths = rge

          importing

            newdate    = olddate.

2. call function 'TV_GO_BACK_N_DAYS'

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
__River__
2011-07-05 · 超过13用户采纳过TA的回答
知道答主
回答量:40
采纳率:0%
帮助的人:35万
展开全部

CALL FUNCTION 'MONTH_PLUS_DETERMINE'

  EXPORTING

    MONTHS        = '-1'

    OLDDATE       = sy-datum

  IMPORTING

    NEWDATE       = g_date.

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式