Converting java.lang.String to oracle.jbo.domain.Date

Requirement- to convert date as a string to oracle.jbo.domain.Date.

Solutions-

/**
*Converts a String to oracle.jbo.domain.Date
* @param String
* @return oracle.jbo.domain.Date
*/
public oracle.jbo.domain.Date castToJBODate(String aDate){
          DateFormat formatter;
          java.util.Date date; 

          if(aDate!=null){

            try {

              formatter = new SimpleDateFormat("dd/MMM/yyyy");
              date = formatter.parse(aDate);
              java.sql.Date sqlDate = new java.sql.Date(date.getTime());
              oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(sqlDate);

                return jboDate;
               } catch (ParseException e) {

                        e.printStackTrace();
                }

         }

         return null;
}

Another scenario –Convert oracle.jbo.domain.Date to String

Use this code-

 public String convertJbodateToString(oracle.jbo.domain.Date domainDate){
        
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");  
        Date date = null;
        
        try {
            date = formatter.parse(domainDate.toString().substring(0, 21));
        } catch (ParseException e) {
        }
        
                
        SimpleDateFormat FORMATTER = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        System.out.println("NewDate-->" + FORMATTER.format(date));
        
        return FORMATTER.format(date);
    }

Happing coding with Vinay in techartifact

Get current date, time , timestamp in Java

You can get the current date time via following two classes – Date and Calendar. And, later use SimpleDateFormat class to convert the date into user friendly format.

-> Date() + SimpleDateFormat()

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

-> get the time

Date date = new Date();
System.out.println(date.getTime());

-> get the time stamp

Date date = new Date();
System.out.println(new Timestamp(date.getTime()));

You can define various format i.e

-> dd-MMM-yy HH:mm:ss
-> yyyy/MM/dd HH:mm:ss
-> yy/MM/dd HH:mm:ss

You can use as working code like

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
 
public class GetCurrentDateTime {
  public static void main(String[] args) {
 
	   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	   //get current date time with Date()
	   Date date = new Date();
	   System.out.println(dateFormat.format(date));
            
           //get the time
           Date date = new Date();
	   System.out.println(new Timestamp(date.getTime()));

           // get the timestamp
 
  }
}