convert String to Calendar
How do you convert a String in yyyyMMddHHmmss format to a Calendar value so it can be edited?
I've been looking at the SimpleDateFormat, the Calendar and the Date APIs and haven't been able to figure out how to do it
What is the format I use for formatting the String to whatever it is that Calendar can read?
So, I have temp, which grabs the attribute data from an XML file, and I'm trying to convert it to a Date so it can be converted into a Calendar (I'm not sure if this is the correct approach):
Code:
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat timeFormatStored = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = frontSplitString((attr.getValue(i).toString()).trim());
java.util.Date date1 = null;
try {
date1 = inputFormat.parse(temp);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar startthis = date1.getDate();
However, I get an error when I try this saything that I cannot convert from int to Calendar.
I've also tried:
Code:
Calendar startthis = timeFormatStored.format(date1).getDate();
But I get this error: The method getDate() is undefined for the type String MyHandler.java
And also:
Code:
Calendar startthis = timeFormatStored.parse(temp);
Error: Cannot cast from Date to Calendar MyHandler.java
Code:
Calendar startthis = Timestamp.valueOf(temp);
That doesn't work either.
Error: Type mismatch: cannot convert from Timestamp to Calendar MyHandler.java
Where does the problem lay? Or, what am I missing? What is the code that makes it work?
I posted this here a couple of weeks ago....
....maybe it can give you some hints.
Code:
/**
* Date difference, as minutes (abs)
*/
import java.util.*;
import java.text.*;
import javax.swing.text.*;
public class DateDiff {
static SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
private String sD1;
private String sD2;
public DateDiff(String sD1, String sD2) {
this.sD1=sD1;
this.sD2=sD2;
}
/**
* Parse date strings in format like: 2005-01-01 22:10:35
*/
public long getDifferenceInMinutes () throws ParseException {
Date d1=df.parse(sD1);
Date d2=df.parse(sD2);
long d1Ms=d1.getTime();
long d2Ms=d2.getTime();
return Math.abs((d1Ms-d2Ms)/60000);
}
public Calendar dateString2Calendar(String s) throws ParseException {
Calendar cal=Calendar.getInstance();
Date d1=df.parse(s);
cal.setTime(d1);
return cal;
}
/**
* Parse date strings in format like: 2005-01-01 22:10:35
* No freeebies !
*/
public long getDifferenceInMinutesStrict () throws ParseException {
Date d1=df.parse(sD1);
Date d2=df.parse(sD2);
long d1Ms=d1.getTime();
long d2Ms=d2.getTime();
return Math.abs((d1Ms-d2Ms)/60000);
}
/**
***************** MAIN *******************
* @param args
*/
public static void main(String[] args) {
DateDiff dd = new DateDiff("2006-01-01 22:10:35",
"2005-01-01 25:11:35");
try {
long diff=dd.getDifferenceInMinutes();
System.out.println("Time difference (abs): " + diff);
diff=dd.getDifferenceInMinutesStrict();
System.out.println("Time difference (abs): " + diff);
}
catch (ParseException ex) {
ex.printStackTrace();
}
}
}