aspose file tools*
The moose likes Java in General and the fly likes Java Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Date" Watch "Java Date" New topic
Author

Java Date

Arvind Chavar
Ranch Hand

Joined: May 16, 2001
Posts: 53
I want to create a Java Date Object from a string in the format
CCYYMMDD.
Example :
String dateStr = "19700120";
Above date represents Jan 20 1970.How do I create a Date(java.util.date) object from this.
Thanks,
Arvind
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Arvind.
Something like this should give you what you're after:

For an explanation of which characters may be passed to the constructor of java.text.SimpleDateFormat and what they mean, check out here.
Good luck, Arvind.
Art
Julio Lopez
Greenhorn

Joined: Nov 14, 2000
Posts: 28
I don't know of any elegant way to do this what I would do is,
1. parse the date String into its constituent parts
2. create ints from the parts
3. create an instance of java.util.Calendar
4. set the Calendar time with the ints
5. get the long value of the date and use that when you create the instance of the java.util.Date
try {
int theYear = java.lang.Integer.parseInt(dateStr.substring(0,4));
int theMonth = (java.lang.Integer.parseInt(dateStr.substring(4,6))) - 1; // subtract one month because month 0 is January
int theDay = java.lang.Integer.parseInt(dateStr.substring(6,8));
java.util.Calendar theCalendar = java.util.Calendar.getInstance();
theCalendar.set(theYear, theMonth, theDay);
java.util.Date theDate = theCalendar.getTime();
}
You could avoid using the java.util.Calendar class and just calculate the long value of the date when you get the int values of the year, month and day.
Julio Lopez
M-Group Systems
[This message has been edited by Julio Lopez (edited July 13, 2001).]
Damien Malone
Greenhorn

Joined: Jul 12, 2001
Posts: 7
Hi
I am doing something similar, but I want to go on from there day by day. Here is an example of what I am doing
import java.util.*;

public class test
{

static public void main(String args[])
{

Calendar d1 = convertToDate("20001205");
Calendar d2 = convertToDate("20001231");

while(d1.getTime().getTime()<=d2.getTime().getTime())
{
int month = d1.get(Calendar.MONTH)+1;
int dayOfMonth = d1.get(Calendar.DAY_OF_MONTH);

d1.set(Calendar.DAY_OF_MONTH, d1.get(Calendar.DAY_OF_MONTH)+1);

String dateString = new Integer(d1.get(Calendar.YEAR)).toString();
if(month<10)
dateString = dateString + "0";

dateString = dateString + month;
if(dayOfMonth<10)
dateString = dateString + "0";

dateString = dateString + dayOfMonth;


System.out.println("ADDING :" +dateString);

}//emnd while



}//end main


static public Calendar convertToDate(String date)
{
Calendar d = new GregorianCalendar();
d.set(new Integer(date.substring(0, 4)).intValue(),(new Integer(date.substring(4, 6)).intValue())-1,new Integer(date.substring(6, 8)).intValue(),9,9);

return d;
}

}
This outputs
ADDING :20001221
ADDING :20001222
ADDING :20001223
ADDING :20001224
ADDING :20001225
ADDING :20001226
ADDING :20001227
ADDING :20001228
ADDING :20001229
ADDING :20001230
ADDING :20011231
It works fine most of the time, but if you look at what it says for the last output, it goes to 20011231 instead of 20001231. Does anyone know why it goes to 2001. I've tried setting a default time that is during the day and that doesnt fix it.
Any help would be appreciated
Damien
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi,
there is a DateFormat class in java.text.
it's method parse(String text) alows to create a Date object from a String. perhaps you can find something there.
pascal
Larry Towle
Greenhorn

Joined: Jun 20, 2001
Posts: 2
Also check out Roedy Green's essay on Dates and Calendars at the following address: http://www.mindprod.com/calendar.html
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Have you seen java.util.JulianCalendar.Maybe that would help!
-- Sandeep
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Here's a method that will do it.
public static Date convertStringToDate(String inStr)
{
int year= Integer.parseInt(inStr.substring(0,4));
int month= Integer.parseInt(inStr.substring(4,6)) - 1;
int day= Integer.parseInt(inStr.substring(6,8));
Calendar cal = Calendar.getInstance();
cal.set(year, month, day, 0, 0, 0);
return cal.getTime();
}
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I've never seen the JulianCalendar class. Do you mean GregorianCalendar?
Originally posted by Desai Sandeep:
Have you seen java.util.JulianCalendar.Maybe that would help!
-- Sandeep

Arvind Chavar
Ranch Hand

Joined: May 16, 2001
Posts: 53
Hi Guys,
Thnaks for so many elegant solutions!!.As far as Pradeep's reply to my query,even I was not able to find JulianCalendar.I think he meant Gregorian.. and not Julian...
Thanks,
Arvind
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Damien, As for your problem, you are assigning values to month and day before incrementing the day. You should change your code as follows:
import java.util.*;
public class test
{
static public void main(String args[])
{
Calendar d1 = convertToDate("20001205");
Calendar d2 = convertToDate("20001231");
while(d1.getTime().getTime()<=d2.getTime().getTime())
{
//move from here
//int month = d1.get(Calendar.MONTH)+1;
//int dayOfMonth = d1.get(Calendar.DAY_OF_MONTH);
d1.set(Calendar.DAY_OF_MONTH, d1.get(Calendar.DAY_OF_MONTH)+1);
//to here
int month = d1.get(Calendar.MONTH)+1;
int dayOfMonth = d1.get(Calendar.DAY_OF_MONTH);
String dateString = new Integer(d1.get(Calendar.YEAR)).toString();
if(month<10)
dateString = dateString + "0";
dateString = dateString + month;
if(dayOfMonth<10)
dateString = dateString + "0";
dateString = dateString + dayOfMonth;

System.out.println("ADDING :" +dateString);
}//emnd while
}//end main
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Uh guys, Art Metzer's post solved this whole problem elegantly and concisely way back in post number two. Read the replies before posting. I realize everyone wants to win a copy of Josh's book, but y'all are just reinventing the wheel. The SimpleDateFormat class was created for exactly this sort of problem.

"I'm not back." - Bill Harding, Twister
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Jim hit the nail on the head. In fact, Art's program can be made even more concise. The catch block merely prints out the exception. The interpreter will do that for you if you let the exception propagate out of main, so the following tiny program does the trick:
<pre>
import java.text.*;
import java.util.*;

public class StringToDate {
public static void main( String[] args ) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
System.out.println( "Date: " + sdf.parse("197foo00120"));
}
}
</pre>
This program prints the entire stack trace (Art's program just prints the exception itself). Catching the exception would be better form if you could do something more useful than simply printing the stack trace, but in this case you can't. Also, the program will never throw the exception; the only date being parsed is hardwired and correct.
As a general rule, programmers tend to catch exceptions more often than necessary. Assuming an exception is appropriate to the abstraction offered by the method that invokes the method that throws the exception (Item 43), it's generally better and easier simply to declare that the enclosing method also throws the exception. This avoids clutter and allows the method that invokes the enclosing method to decide how best to deal with the exception.

------------------
Joshua Bloch
Author of Effective Java
[This message has been edited by Joshua Bloch (edited July 12, 2001).]
[This message has been edited by Joshua Bloch (edited July 12, 2001).]


Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
Damien Malone
Greenhorn

Joined: Jul 12, 2001
Posts: 7
Chad, that works perfect, thanks
Damien
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Originally posted by Paul Keohan:
I've never seen the JulianCalendar class. Do you mean GregorianCalendar?

Yes that is correct!
-- Sandeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Date