need help, how to convert this in java? 09-09-2012 = September 9, 2012?
our instructor give it as an assignment.. Month, day, and year and inputted using separately
thnx in advance
package exam4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class Exam4 {
public static void main(String[] args) {
String inMonth=JOptionPane.showInputDialog(null, "Enter Month");
int intMonth = Integer.parseInt(inMonth);
String inDay=JOptionPane.showInputDialog(null, "Enter Day");
int intDay = Integer.parseInt(inDay);
String inYear=JOptionPane.showInputDialog(null, "Enter Year");
int intYear = Integer.parseInt(inYear);
getDate(intMonth, intDay, intYear);
}
static void getDate(int intMonth, int intDay, int intYear){
if((intMonth==2)&&(intDay<=29)&&(intYear<=9999)){
JOptionPane.showMessageDialog(null, "February 29, 2012");
genDate(intMonth,intDay,intYear);
}
else if (((intMonth<12)&&(intDay<31)&&(intYear<9999))&&((intMonth==2)&&(intDay<=29)&&(intYear<=9999))){
JOptionPane.showMessageDialog(null, "Regular date");
}
else{
JOptionPane.showMessageDialog(null, "atik na date");
}
}
static void genDate(int intMonth, int intDay, int intYear){
String fMonth = Integer.toString(intMonth);
String fDay = Integer.toString(intDay);
String fYear = Integer.toString(intYear);
String DateTime=fMonth+fDay+fYear;
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(DateTime);
} catch (ParseException e) {
}
SimpleDateFormat timeFormat = new SimpleDateFormat("MMMMM dd, yyyy");
String finalDate = timeFormat.format(myDate);
JOptionPane.showMessageDialog(null,finalDate);
}
}
when i inputted the 02 02 2012, it gives me weird date… try to enter 02 29 2012.. and it gives december 13, 0013.. i dnt knw why =(
Use
SimpleDateFormatto convert the string to date and then convert back to String as below: