import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class GetDate extends Applet {
private boolean isStandalone = false;
Choice choiceDay = new Choice();
Choice choiceOccurrence = new Choice();
Label label3 = new Label();
Choice choiceMonth = new Choice();
FlowLayout flowLayout = new FlowLayout();
BorderLayout borderLayout = new BorderLayout();
Panel panelTop = new Panel();
Panel panelCenter = new Panel();
Label labelResult = new Label();
Label label2 = new Label();
TextField textFieldYear = new TextField();
Button buttonGetDate = new Button();
public GetDate() {
}
public void init() {
try {
myInit();
choiceOccurrence.add("First");
choiceOccurrence.add("Second");
choiceOccurrence.add("Third");
choiceOccurrence.add("Fourth");
choiceOccurrence.add("Fifth");
choiceOccurrence.add("Sixth");
choiceOccurrence.add("Seventh");
choiceDay.add("Sunday");
choiceDay.add("Monday");
choiceDay.add("Tueday");
choiceDay.add("Wednesday");
choiceDay.add("Thurday");
choiceDay.add("Friday");
choiceDay.add("Saturday");
choiceMonth.add("January");
choiceMonth.add("February");
choiceMonth.add("March");
choiceMonth.add("April");
choiceMonth.add("May");
choiceMonth.add("June");
choiceMonth.add("July");
choiceMonth.add("August");
choiceMonth.add("September");
choiceMonth.add("October");
choiceMonth.add("November");
choiceMonth.add("December");
Calendar tvCalendar = Calendar.getInstance();
textFieldYear.setText("" + tvCalendar.get(Calendar.YEAR));
}
catch(Exception e) {
e.printStackTrace();
}
}
private void myInit() throws Exception {
this.setBackground(Color.white);
this.setLayout(borderLayout);
label3.setText("of");
labelResult.setFont(new java.awt.Font("Dialog", 1, 16));
labelResult.setText("Usage: Select your query and click the \"Get Date\" button.");
label2.setText("year");
textFieldYear.setText("textField1");
buttonGetDate.setLabel("Get Date");
panelTop.setLayout(flowLayout);
panelTop.add(choiceOccurrence, null);
panelTop.add(choiceDay, null);
panelTop.add(label3, null);
panelTop.add(choiceMonth, null);
panelTop.add(label2, null);
panelTop.add(textFieldYear, null);
panelTop.add(buttonGetDate, null);
panelCenter.setLayout(flowLayout);
panelCenter.add(labelResult, null);
this.add("North", panelTop);
this.add("Center", panelCenter);
}
public String getAppletInfo() {
return "Stupid Date Trick";
}
public boolean action(Event e, Object o) {
if (e.target.equals(buttonGetDate)) {
getDate();
}
return true;
}
private void getDate() {
Calendar tvCalendar = Calendar.getInstance();
String tvYear = textFieldYear.getText();
int tvMonth = choiceMonth.getSelectedIndex();
int tvDay = choiceDay.getSelectedIndex() + 1;
int tvOccurrence = choiceOccurrence.getSelectedIndex() + 1;
if (tvYear != null && tvYear.length() > 0) {
try {
tvCalendar.set(Integer.parseInt(tvYear), tvMonth, 1, 0, 0, 0);
while (tvCalendar.get(Calendar.DAY_OF_WEEK) != tvDay) {
tvCalendar.add(Calendar.DAY_OF_WEEK, 1);
}
for (int i = 0; i<tvOccurrence - 1; i++) {
for (int j=0; j<7; j++) {
tvCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
if (tvCalendar.get(Calendar.MONTH) == tvMonth) {
SimpleDateFormat tvDateFormat = new SimpleDateFormat("EEE, MMM dd, yyyy");
setResult(tvDateFormat.format(tvCalendar.getTime()));
System.out.println(tvCalendar.getTime().toString());
} else {
setResult("Date overlaps months.");
}
} catch (Exception e) {
setResult("Invalid Year.");
}
} else {
setResult("Invalid Year.");
return;
}
}
private void setResult(String message) {
labelResult.setText(" " + message);
}
}