Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:20:54+00:00 2026-05-27T11:20:54+00:00

I am reading a date column from the Oracle DB. The column type is

  • 0

I am reading a date column from the Oracle DB.

The column type is Date.

Example Date data is: 2011-12-06 14:28:12

Now when I am reading this date I want to split the date part into a separate String and Time part into another.

// here’s the code I am working with.

DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss"); 

// some loop.
    SomeBean bean =  new SomeBean();
    Date date  = convertTimestamp(rs.getTimestamp("DATE_VAL"));
    bean.setTime(date==null?"":convertToTimeStamp(date,true));
    bean.setDate(date==null?"": df.format(date));           
// end some loop.

private java.util.Date convertTimestamp(java.sql.Timestamp timestamp) {
    if (timestamp == null)
        return null;
    else
        return new java.util.Date(timestamp.getTime());
}

private  String convertToTimeStamp(Date date, boolean showSeconds) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    return convertToTimeStamp(c, showSeconds);
}   

public static String convertToTimeStamp(Calendar time, boolean showSeconds) {
    String hours = Integer.toString(time.get(Calendar.HOUR_OF_DAY));
    if (hours.length() == 1) {
        hours = '0' + hours;
    }
    String minutes = Integer.toString(time.get(Calendar.MINUTE));
    if (minutes.length() == 1) {
        minutes = '0' + minutes;
    }
    if (showSeconds) {
        String seconds = Integer.toString(time.get(Calendar.SECOND));
        if (seconds.length() == 1) {
            seconds = '0' + seconds;
        }
        return hours + ":" + minutes + ":" + seconds;
    } else {
        return hours + ":" + minutes;
    }
}

Please help. Your help will be appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:20:55+00:00Added an answer on May 27, 2026 at 11:20 am

    first of all I would recommend to have a look at Joda-Time: it has a lot of easy going date and time functions. (a lot easier than the java date).

    converting java.sql.Date to joda DateTime I use:

    public static DateTime convertSqlDateToDateTime(java.sql.Date sqlDate){
        Calendar c = Calendar.getInstance();
        c.setTime(sqlDate);
        //note: java months are between 0 and 11:
        DateTime dt=new DateTime(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),c.get(Calendar.SECOND),c.get(Calendar.MILLISECOND));
        return dt;
    }
    

    This will at least save you one function, and will maybe help you to your solution.

    To convert a java.sql.TimeStamp to joda DateTime:

    public static DateTime convertSqlTimeStampToDateTime(java.sql.Timestamp sqlTimestamp){
        Calendar c = Calendar.getInstance();
        c.setTime(sqlTimestamp);
        //note: java months are between 0 and 11:
        DateTime dt=new DateTime(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),c.get(Calendar.SECOND),c.get(Calendar.MILLISECOND));
        return dt;
    }
    

    Second of all, why not use the DateFormatter to convert your date to a string you want?

    Sticking to the Joda DateTime, use:

    /**
     * Converts a DateTime to a string using the given pattern (format)
     * @param dateTime
     * @param pattern
     * @return
     */
    public static String convertDateTimeToPattern(DateTime dateTime,String pattern){
        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return dateTime.toString(fmt);
    }
    

    U can use the same formats as the java.Date and Time formatters:

    pattern = “DD-MM-YYYY” (dutch notation) or “YYYY-MM-DD” (uk notation) and for time pattern=”hh:mm:ss”

    if you want to stick to your method, create another SimpleDateFormat, and use this last pattern (“hh:mm:ss”) to create a time String.

    Please, at leaste change this function:

    public static String convertToTimeStamp(Calendar time, boolean
    showSeconds)

    in to:

    public static String convertToTimeStamp(Date date, boolean showSeconds) {
        String pattern = "hh:mm:ss";
        if (!showSeconds) {
            pattern = "hh:mm";
        }
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }
    

    In your last remark, i gues you asked for:

    public static String convertToDateString(Date date) {
        String pattern = "YYYY-MM-DD"; 
        //(or another date format, like in dutch: "DD-MM-YYY"
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel formula reading data from a column. The data in that
I have a date I'm reading from an API in the following format: 2010-03-15T00:00:00-04:00
I am reading data from an excel sheet and displaying it in a data
I recall hearing or reading somewhere that SELECT * from TABLE where date >=
I am reading data from an xls spreadsheet with xlrd. First, I gather the
I have a query reading a date from a parent/child relationship and i need
I am pasting data from a US webpage into an excel sheet. A European
I have a query like this: SELECT TOP 1 ID, DATA, OTHERINF FROM MYTABLE
I have a table1 like this: Date Reading Cost 2009-01-01 5.00 1500.00 2009-01-02 10.00
I have a C# app that is reading data from a stored procedure and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.