TO_CHAR(sysdate, 'DD/MM/YYYY HH24:MI:SS') can be used in H2 database. This Date Conversion from Oracle Format to java format can be used for normal java applications also.
DROP alias if exists TO_CHAR;
CREATE alias TO_CHAR as $$
import java.text.SimpleDateFormat;
import java.util.Date;
@CODE
String toChar(String date, String pattern) throws Exception {
pattern = pattern.replaceAll("YY","yy");
pattern = pattern.replaceAll("DD","dd");
pattern = pattern.replaceAll("HH24|hh24","HH");
pattern = pattern.replaceAll("HH?!24|hh?!24","KK");
pattern = pattern.replaceAll("MON|mon","MMM");
pattern = pattern.replaceAll("MI|mi","mm");
pattern = pattern.replaceAll("SS|ss","ss");
pattern = pattern.replaceAll("AM|PM","aa");
SimpleDateFormat sm = new SimpleDateFormat(pattern);
java.util.Date dt;
if(date.length() > 10)dt = java.sql.Timestamp.valueOf(date);
else
dt = java.sql.Date.valueOf(date);
return sm.format(dt);
}
$$
create table test(bday timestamp);
insert into test values(today);
select TO_CHAR(bday,'DD/MM/YYYY HH24:MI:ss') from test;
create table test2(bday DATE);
insert into test2 values(today);
select TO_CHAR(bday,'DD/MM/YYYY HH24:MI:ss') from test2;
Thanks a lot...
ReplyDeletethis works in spring as below.
@Transactional
public void toChar() {
final String script = "drop alias if exists TO_CHAR;create alias TO_CHAR as $$import java.text.SimpleDateFormat;import java.util.Date;@CODEString toChar(String date, String pattern) throws Exception {pattern = pattern.replaceAll(\"YY\",\"yy\");pattern = pattern.replaceAll(\"DD\",\"dd\");pattern = pattern.replaceAll(\"HH24|hh24\",\"HH\");pattern = pattern.replaceAll(\"HH?!24|hh?!24\",\"KK\");pattern = pattern.replaceAll(\"MON|mon\",\"MMM\");pattern = pattern.replaceAll(\"MI|mi\",\"mm\");pattern = pattern.replaceAll(\"SS|ss\",\"ss\");pattern = pattern.replaceAll(\"AM|PM\",\"aa\");SimpleDateFormat sm = new SimpleDateFormat(pattern);java.util.Date dt; if(date.length() > 10)dt = java.sql.Timestamp.valueOf(date);else dt = java.sql.Date.valueOf(date);return sm.format(dt);}$$";
final EntityManager em = new Subscription().getEntityManager();
em.createNativeQuery(script).executeUpdate();
}