I am reading in a String from a text file which contains a date in the form of yyMMdd I then want to convert it to type date but when I do that it loses its format. here is an exmaple of what I have tried
String strDate = matcher.group(10); // strDate would contain 111107
SimpleDateFormat formatter1 = new SimpleDateFormat("yyMMdd");
Date date = formatter1.parse(strDate); // after parsing it, the format changes to Thu Nov 03 00:00:00 EDT 2011
But if I take date and put it back into a string like so String tDate = formatter1.format(date); the string then contains the date in the format Id like to see 111107.
Is there a way I can do this? maybe if I could some how call the format function to return a date object instead of String, thanks.
Edit
I read a list of dates in from a file and load them into a map I then compare those dates to the current date which is also in the yyMMdd format and then if the date from the map is more then a week earlier than the current date I prompt the user for input and then write the date and other related info to a file, in the yyMMdd format. The reason I use a map is each line of the text file contains some information and a unique name, and I compared the date for that specific line of data so I do a map.get(aName)
here is the code, hope it helps
dbConnect();
stmt = conn.createStatement();
String query = "select * from OBJECT_BAC_EV where instance = 12";//VALUE <> 'Normal'";
rslt = stmt.executeQuery(query);
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
String dateNow = formatter.format(currentDate.getTime());
Date curDate = (Date)formatter.parse(dateNow);
currentDate.setTime(curDate);
currentDate.add(Calendar.DAY_OF_MONTH, -7);
dateNow = formatter.format(currentDate.getTime());
curDate = (Date)formatter.parse(dateNow);
Calendar cDate = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
String strDate = f.format(cDate.getTime());
while(rslt.next())
{
System.out.println(rslt.getRow());
String aValue = rslt.getString("VALUE");
String aName = rslt.getString("NAME");
String aObjRef = rslt.getString("ObjRef");
if(aNoteMap.containsKey(aName))
{
String n = aNoteMap.get(aName);
if(aDateMap.get(aName).before(curDate))
{
int answer = JOptionPane.showConfirmDialog(null, "Would you like to use last weeks note? " + n, "Hey", JOptionPane.YES_NO_OPTION);
if( answer == JOptionPane.YES_OPTION)
{
output.write(aName + " " + aObjRef + " " + aValue + " " + aDateMap.get(aName) + " "
+ n + (System.getProperty("line.separator")));
}
else if( answer == JOptionPane.NO_OPTION)
{
String newNote = JOptionPane.showInputDialog("Enter new note");
output.write(aName + " " + aObjRef + " " + aValue + " " + aDateMap.get(aName) + " "
+ newNote + (System.getProperty("line.separator")));
}
}
else
output.write(aName + " " + aObjRef + " " + aValue + " " + aDateMap.get(aName) + " "
+ n + (System.getProperty("line.separator")));
}
else
output.write(aName + " " + aObjRef + " " + aValue + " " + strDate + " "
+ "" + (System.getProperty("line.separator")));
}
System.out.println("its closing output..");
output.close();
}
The Date class has no “internal” format, it only represents date elements. To output it using a specific format, you need to do the way you did:
String tDate = formatter1.format(date);The reason why you think it has the “wrong format” is probably because when you try to output it, it does a
toString()by default, which doesn’t format it the way you want.If you give us more details about how you want to use that date (include your code), then we might be able to provide suggestions on how to inject the formatter into it.