LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2006, 12:48 AM   #1
Kamikazee
Member
 
Registered: May 2005
Location: Aus
Distribution: SimplyMEPIS 3.3
Posts: 107

Rep: Reputation: 15
Java - Convert String to Date and back


Ive done a bit of VB.NET work before, and doing java for computer science at uni. What im trying to do is convert a string formated "DD/MM/YYYY HH:MM" to a string formated like "'WEDNESDAY, 14TH APRIL 2006" .

The class Timestamp is used to represent a time in place. Initlizing it with the time/date. Then being able to use the method .toString() to get the date in full.

I dont think im parsing the date as a string properly, and when i run it as is, it doesnt throw an exception. However if i run it with myDateTimeGroup.toString() as the return value, it throws exception null. Hence why i dont think the date (as string) is being parsed properly. Any ideas?


Code:
/ CONSTRUCTOR - STRING MUST BE 'DD/MM/YYYY HH:MM'
	public Timestamp(String datetime) {
		// SPLIT DATETIME INTO VARIABLES AND SET THEM [working]
		myDate = Integer.parseInt(datetime.substring(0, 2));
		myMonth = Integer.parseInt(datetime.substring(3, 5));
		myYear = Integer.parseInt(datetime.substring(8, 10));
		myHour = Integer.parseInt(datetime.substring(11, 13));
		myMinute = Integer.parseInt(datetime.substring(14, 16));

		

	}


	// RETURNS STRING FORMATED LIKE - 'WEDNESDAY, 14TH APRIL 2006'
	public String toString() {
		DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
		ParsePosition pos;
		try {
			pos = new ParsePosition(0);
			myDateTimeGroup = df.parse(String.valueOf(myDate) + "/"
					+ String.valueOf(myMonth) + "/" + String.valueOf(myYear),
					pos);
					
			return " ";
		} catch (Exception e) {
			return "ERROR - " + e.getMessage();
		}
	}
 
Old 04-27-2006, 02:52 AM   #2
Kamikazee
Member
 
Registered: May 2005
Location: Aus
Distribution: SimplyMEPIS 3.3
Posts: 107

Original Poster
Rep: Reputation: 15
disregard. solution found.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ parse date string?? blizunt7 Programming 25 05-18-2006 03:12 PM
How to convert string to wchar_t krishnacins Programming 1 03-28-2006 04:21 AM
convert date time format given in string suchi_s Programming 6 01-10-2005 04:06 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration