File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Date

 
Arvind Chavar
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen java.util.JulianCalendar.Maybe that would help!
-- Sandeep
 
Chad McGowan
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joshua Bloch
Author and "Sun God"
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Damien Malone
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chad, that works perfect, thanks
Damien
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Keohan:
I've never seen the JulianCalendar class. Do you mean GregorianCalendar?

Yes that is correct!
-- Sandeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic