Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Date Conversion in Java. Show all posts
Showing posts with label Date Conversion in Java. Show all posts

Thursday, 7 November 2024

Working with Dates in Oracle ADF and Java

Get Current JBO Date with Time 



Sometimes we need to get and set the current date in our Oracle ADF code

To get the current date in an Oracle ADF application using oracle.jbo.domain.Date, we can use this code, this code can be written in model implementation classes or in Managed Bean





 /**Method to get current JBO Date with time part
     * @return
     */
    public Date getCurrentJboDate(){
        Date currentDate = (Date) Date.getCurrentDate();
        return currentDate;
    }



Get the current JBO Date in a Specific format (JBO Date without time part)


There are times when you need to work with just the date and not the time, whether it's for formatting purposes or for cleaner data extraction. In such cases, you can use a simple code snippet to retrieve the date in a specific format. Here's how you can do it!

    /**Method to get current JBO Date in Specific format
     * @return
     */
    public Date getformattedJboDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd");
        java.util.Date date = new java.util.Date();
        String date1 = dateFormat.format(date);
        try {
            date = dateFormat.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        oracle.jbo.domain.Date formattedDate = new oracle.jbo.domain.Date(sqlDate);
        return formattedDate;
    }

Convert java.util Date to java.sql Date



/**Method to convert java.util.Date to java.sql.Date * @return */ public java.sql.Date convertToSqlDate() { //Util Date java.util.Date date = new java.util.Date(); //SQL Date java.sql.Date sqlDate = new java.sql.Date(date.getTime()); return sqlDate; }


Convert java.util Date to oracle.jbo.domain Date



/**Method to convert java.util.Date to oracle.jbo.domain.Date * @return */ public Date convertToJboDate() { //Util Date java.util.Date date = new java.util.Date(); //SQL Date java.sql.Date sqlDate = new java.sql.Date(date.getTime()); //JBO Date oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(sqlDate); return jboDate; }

Get current day of week in Java 8 and later



To get the current day of the week in Java, you can use the LocalDate class from the java.time package, which is part of Java 8 and later. Here’s how you can do it:

import java.time.LocalDate; import java.time.format.TextStyle; import java.util.Locale; /**Method to get current day of week * */ public void getCurrentDayOfWeek() { // Get the current date LocalDate currentDate = LocalDate.now(); // Get the day of the week as a string String dayOfWeek = currentDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH); // Print the day of the week System.out.println("Today is: " + dayOfWeek); }

Code to get current day of week using Calendar class is like this

/**Method to get current day of week *using Calendar clas */ public void getCurrentDayOfWeek() { // Get the current date and time Calendar calendar = Calendar.getInstance(); // Get the day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday) int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // Print the corresponding day of the week String dayName = ""; switch (dayOfWeek) { case Calendar.SUNDAY: dayName = "Sunday"; break; case Calendar.MONDAY: dayName = "Monday"; break; case Calendar.TUESDAY: dayName = "Tuesday"; break; case Calendar.WEDNESDAY: dayName = "Wednesday"; break; case Calendar.THURSDAY: dayName = "Thursday"; break; case Calendar.FRIDAY: dayName = "Friday"; break; case Calendar.SATURDAY: dayName = "Saturday"; break; } System.out.println("Today is- " + dayName); }

Get Number of days between 2 JBO Date


/**Method to calculate number of days between two jbo dates * @param date1 * @param date2 */ public void getNumberOfDaysBetweenDates(oracle.jbo.domain.Date date1, oracle.jbo.domain.Date date2) { long numberOfDays = 0; if (date1 != null && date1 != null) { numberOfDays = ((date1.getValue().getTime() - date2.getValue().getTime()) / (24 * 60 * 60 * 1000)); } }