convert String to Calendar


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: convert String to Calendar

  1. #1
    Join Date
    May 2005
    Posts
    3

    convert String to Calendar

    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?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I posted this here a couple of weeks ago....

    ....maybe it can give you some hints.
    Code:
    /**
     * Date difference, as minutes (abs)
     */
    import java.util.*;
    import java.text.*;
    import javax.swing.text.*;
    
    public class DateDiff {
    
      static SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
    
      private String sD1;
      private String sD2;
    
      public DateDiff(String sD1, String sD2) {
        this.sD1=sD1;
        this.sD2=sD2;
      }
      /**
       * Parse date strings in format like: 2005-01-01 22:10:35
       */
      public long getDifferenceInMinutes () throws ParseException {
        Date d1=df.parse(sD1);
        Date d2=df.parse(sD2);
        long d1Ms=d1.getTime();
        long d2Ms=d2.getTime();
        return Math.abs((d1Ms-d2Ms)/60000);
      }
      public Calendar dateString2Calendar(String s) throws ParseException {
        Calendar cal=Calendar.getInstance();
        Date d1=df.parse(s);
        cal.setTime(d1);
        return cal;
      }
      /**
       * Parse date strings in format like: 2005-01-01 22:10:35
       * No freeebies !
       */
      public long getDifferenceInMinutesStrict () throws ParseException {
        Date d1=df.parse(sD1);
        Date d2=df.parse(sD2);
        long d1Ms=d1.getTime();
        long d2Ms=d2.getTime();
        return Math.abs((d1Ms-d2Ms)/60000);
      }
    
      /**
       ***************** MAIN *******************
       * @param args
       */
      public static void main(String[] args) {
        DateDiff dd = new DateDiff("2006-01-01 22:10:35",
                                   "2005-01-01 25:11:35");
        try {
          long diff=dd.getDifferenceInMinutes();
          System.out.println("Time difference (abs): " + diff);
          diff=dd.getDifferenceInMinutesStrict();
          System.out.println("Time difference (abs): " + diff);
        }
        catch (ParseException ex) {
          ex.printStackTrace();
        }
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Posts
    3
    Nope, that wasn't what I was looking for, unfortunately.

    But, I did find what I was searching for while I was googling:

    Calendar startthis = Calendar.getInstance();
    startthis.setTime(inputFormat.parse(temp));
    Last edited by sjalle; 05-16-2005 at 11:52 PM. Reason: ooops..

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    well then you didn't look close enough

    Code:
      /**
      * any valid format definition goes here
      */ 
      static SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
      .
      .
      public Calendar dateString2Calendar(String s) throws ParseException {
        Calendar cal=Calendar.getInstance();
        Date d1=df.parse(s);
        cal.setTime(d1);
        return cal;
      }
    Ifyou had implemented this in your class you could then write:
    Code:
    Calendar startthis = dateString2Calendar(temp):
    Last edited by sjalle; 05-16-2005 at 11:53 PM.
    eschew obfuscation

  5. #5
    Join Date
    Jun 2006
    Posts
    1

    Good Call sjalle.

    Good call sjalle. Your first block of code answered my question, which was the same as the original.

    ~Ross

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center