Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-15-2005, 07:22 PM   #1
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Convert String to Calendar in Java

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?
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Force Flow is offline   Reply With Quote
Old 05-15-2005, 11:17 PM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Nevermind, I *finally* was able to find someone who could answer this question.
Force Flow is offline   Reply With Quote
Old 05-27-2005, 02:58 AM   #3
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 93
Send a message via ICQ to raja Send a message via AIM to raja
just out of curiosity, what was the solution?
raja is offline   Reply With Quote
Old 05-27-2005, 09:17 AM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Sorry, thought I posted it.

Here it is:

PHP Code:
Calendar startthis Calendar.getInstance();
startthis.setTime(inputFormat.parse(temp)); 
Force Flow is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:23 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2