3 Replies - 736 Views - Last Post: 20 December 2011 - 12:46 PM Rate Topic: -----

#1 daniaIT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-December 11

convert string date to intger

Posted 20 December 2011 - 12:10 PM

i have to write calender class which will not use java library classes(date,calender) and this class has a 1. A constructor that takes its year, month, day as parameters and another version that takes all of the properties via parameters; namely, year, month, day, hour, minute and second.2.A constructor that takes a string in the form “2/12/2011”, or “2-12-2011” or “2-12-2011 5:55:32” and it should parse the string and get its values from the string.
I did the first part i have problem with second part.plz i need help !!

public class SimpleCalendar1
{
	private int hour;
	private int minute;
	private int second;
		
	private int day;
	private int month;
	private int year;
	
	private String day1;
	private String month1;
	private String year1;
	
	
	public SimpleCalendar1()
	{
	
	}
	
	public SimpleCalendar1(int day1, int month1, int year1)
	{
		setDay(day1);
		setMonth(month1);
		setYear(year1);
	}
	
	public SimpleCalendar1(int day2, int month2, int year2, int hour2, int minute2, int second2)
	{
		setDay(day2);
	    setMonth(month2);
		setYear(year2);
		setHour(hour2);
		setMinute(minute2);
		setSecond(second2);
	}
	
	public void dateString(String date)
	{
		String[] dateArray = date.split("/");
		
		for (int i = 0; i < dateArray.length; i++) {
			int p = Integer.parseInt????????????????
			
		}
		setDay1(dateArray[0]);
		setMonth1(dateArray[1]);
		setYear1(dateArray[2]);
		
		
		
	}
	
	private void setYear1(String year1) {
		int y = Integer.parseInt(year1);
		year1 = String.valueOf(y);
		
	}

	private void setMonth1(String month1) {
		int m = Integer.parseInt(month1);
		month1 = String.valueOf(m);
		
	}

	private void setDay1(String day1) {
		int d = Integer.parseInt(day1);
		day1 = String.valueOf(d);
	}

	public void setDay(int day)
	{
		
		try
		{
			if(day<1 || day>31)
				throw new Exception();
			this.day = day;
			
		}
		catch(Exception ex)
		{
			System.out.println ("Invalid input!");
		}
	}
	
	public void setMonth(int month)
	{
		
		try
		{
			if(month<1 || month>12)
				throw new Exception();
	this.month = month;
			

		}
		catch(Exception ex)
		{
			System.out.println ("Invalid input!");
		}
	}
	
	public void setYear(int year)
	{
		
		try
		{
			if(year<0)
				throw new Exception();
		this.year = year;
		}
		catch(Exception ex)
		{
			System.out.println ("Invalid input!");
		}
	}
	
	public void setHour(int hour)
	{
		
		try
		{
			if(hour<1 || hour>24)
				throw new Exception();
			this.hour = hour;
			
		}
		catch(Exception ex)
		{
			System.out.println ("Invalid input!");
		}
	}
	
	public void setMinute(int minute)
	{
		
		try
		{
			if(minute<1 || minute>60)
				throw new Exception();
			this.minute = minute;
			
		}
		catch(Exception ex)
		{
			System.out.println ("Invalid input!");
		}
	}
	
	public void setSecond(int second)
	{
		
		try
		{
			if(second<1 || second>60)
				throw new Exception();
			this.second = second;
		}
		catch(Exception ex)
		{
			System.out.println ("Invalid input!");
		}
	}
	
	public int getDay()
	{
		return day;
	}
	
	public int getMonth()
	{
		return month;
	}
	
	public int getYear()
	{
		return year;
	}
	
	public int getHour()
	{
		return hour;
	}
	
	public int getMinute()
	{
		return minute;
	}
	
	public int getSecond()
	{
		return second;
	}
	
	public String toString1()
	{
		String result = day + "/" + month + "/" + year + "  " + hour + ":" + minute + ":" + second; 
		return result;
	}

	@Override
	public String toString() {
		return " day1=" + day1 + "/ month1=" + month1
				+ "/ year1=" + year1 ;
	}
	
	
	
}


Please :code:

This post has been edited by pbl: 20 December 2011 - 12:20 PM
Reason for edit:: Code tags fixed


Is This A Good Question/Topic? 0
  • +

Replies To: convert string date to intger

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: convert string date to intger

Posted 20 December 2011 - 12:25 PM

this does not make sense

private void setYear1(String year1) {
		int y = Integer.parseInt(year1);
		year1 = String.valueOf(y);
		
	}


A method that receives a String named year1
convert that String into a local int variable named y
convert that y into a String and assign the value of that String to the received parameter

Basically that method does nothing

I guess that what you wanted to do is

private void setYear1(String year1) {
	year = Integer.parseInt(year1);   // convert into int in my global variables
	this.year1 = year1;               // save also String version in my instance variables
		
	}


:^:
Happy coding

This post has been edited by pbl: 20 December 2011 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: convert string date to intger

Posted 20 December 2011 - 12:27 PM

There is a String method called split(char)

It will give you an array, so here is what you can do, create 2 arrays, date and time. Then assign the parseInt values of them to each second that you need. So here is what you would do.

String sDate = "2-12-2011";
String[] date = sDate.split('-');
int month = Integer.parseInt(date[0]);
int day = Integer.parseInt(date[1]);
int year = Integer.parseInt(date[2]);


Then do the same for the time except change the parameter to a colon ( : ) since it is a time format.

Put this code in the constructor. I can see that you did part of it already, you just need to follow same procedure for the Time.
Was This Post Helpful? 1
  • +
  • -

#4 daniaIT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-December 11

Re: convert string date to intger

Posted 20 December 2011 - 12:46 PM

Thank You Very Much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1