Skip to main content

Posts

Java : How to convert java.util.Date to java.sql.Date?

 Introduction In this article, we will learn how to convert java.util.Date to java.sql.Date Example: import java.util.Calendar; import java.util.Date; public class DateConversion { public static void main(String[] args) { Date date = Calendar.getInstance().getTime(); java.sql.Date sqlDate = new java.sql.Date( date.getTime()); } } Please note that both these Date classes are outdated now. Use java.Time  classes instead of legacy java.util.Date & java.sql.Date with JDBC 4.2 or later. Use Instant class instead of java.util.Date Use LocalDate instead of java.sql.Date

Java : How to convert java.util.Date to Gregorian Calendar Date Format?

Introduction Below example demonstrates how to convert java.util.Date to Gregorian Calendar  Example import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateToGregorianCalendar { public static void main(String[] args) { Date date = Calendar.getInstance().getTime(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(date); } }

ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view

 Problem ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view 12054. 00000 -  "cannot set the ON COMMIT refresh attribute for the materialized view" *Cause:    The materialized view did not satisfy conditions for refresh at commit time. *Action:   Specify only valid options. Solution  You can not use the DISTINCT keyword in your Materialized View query, rather use Group By instead of distinct You can not use the standard JOIN in your query, instead, you should use the old-styled Join like A, B where A.ID = B.ID     

Spring Boot Maven plugin - How to deploy WAR to an external tomcat's webapp folder?

 Introduction Spring Boot Maven plugin by default generates the WAR file inside the target folder. In this post, I will explain how to copy the generated war file to an external Tomcat's webapps folder using Spring Boot Maven plugin. In my earliest post , you can learn how to package a Spring Boot application as WAR.  Solution Step 1  Follow this link to specify the packaging as WAR in pom.xml and also to configure the  SpringBootServletInitializer class Step 2 (optional) Specify a cleaner name for WAR using the finalName tag in pom.xml  <finalName>auth</finalName> Step 3 Specify the outputDirectory directory      <build> <finalname>auth</finalname> <plugins> <plugin>           <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <configuration>           <outputdirectory>D:/Development Utils/apache-tomcat-9.0.30/webapps</outputdirectory>

Error: Could not find or load main class in IntelliJ IDE 2020.1.3

 Error Could not find or load main class in IntelliJ IDE 2020.1.3 Solution Step 1 Right-click on the source folder  src (Normal Java projects) java ( Java Maven project) scala (Scala Maven projects) Step 2 Select Mark Directory As Step 3 Select Sources Root IntelliJ Mark Directory as Sources Root Hope this post helps you to solve your problem.

How to get the length of a Collection in the JSF expression language?

 Problem How to get the length of a Collection in the JSF expression language? Solution There are two ways to get the length of a Collection i.e. List, Set, etc in the JSF expression language a) Define a method in the Bean In your bean declare a method to return the length of a collection @Named("MyBean ") @SessionScoped public class MyBean {     private List list;     .     .     .     public int getCollectionLength() {       return  list.size();     } } b) Using Facelets,the length function #{ fn:length(MyBean.list) }

How to convert Enum to List of String in Java 8?

Problem  How to convert Enum to List of String in Java 8?  Solution  Using the Java 8 Streams, its map and collect functions we can convert Enum to List of Strings  Code   import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class EnumToStringList { public enum Alpha { A,B,C,D; }  public static void main(String[] args) { List<String> strings = Stream.of(Alpha.values()).map(                                               Alpha::name).collect( Collectors.toList()); } }

Java 13 - How to Concatenate Text Blocks

Introduction If you have not tried the Text Blocks feature introduced in Java 13 then the following post will help you to get started Java 13 - Example to use Text Blocks for Multi line String literals We can concatenate TextBlocks as we do with normal String as demonstrated in the following example. Example public class TextBlock { public static void main(String[] args) { String html = """             <html>                 <body>                  """ +                 """   <h1> Header One </h1>                 <p> Paragraph One </p>               """ +                 """                 <h1> Header One </h1>                 <p> Paragraph One </p>       """ +                """                 </body>              </html>                 """; S

Java 13 - Example to use Text Blocks for Multi line String literals

Introduction Lengthy Strings in Java code becomes hard to read. Multiline String literals can be XML, JSON, HTML, SQL queries, Hibernate or JPA queries, etc. Example String html = " <html>\r\n" + " <body>\r\n" + " <p>Hello, world</p>\r\n" + " </body>\r\n" + " </html>\r\n" ; Solution Thanks to the Text blocks feature introduced in Java 13. Now, these multiline String literals are more presentable in Java code. 1. Configuring Eclipse for Text Blocks 1.1 Requirments JDK 13 Eclipse Version: 2020-03 (4.15.0) 1.2 Eclipse Error 1 String literal is not properly closed by a double-quote 1.2.1 Solution Change the source of your program to 13 as shown in the following screenshot Java Source 13 1.3 Eclipse Error 2 Text Blocks is a preview feature and disabled by default. Use --enable-preview to enable 1.3.2 Eclipse Erro