Java Oracle date - jdbc

String sql="select id from period where '"+systemDate+"' between startDate and endDate";
I need to execute this query and I am using oracle 10g. I want to get the id which falls between these dates. Problem I am facing is in oracle my date format is"dd-mon-yy" and systemDate is in format yyyy-mm-dd.So is ther any way to convert java date to dd-mon-format or any other way to execute the above query...
Kindly help.

You should convert your java.util.Date to a java.sql.Date and use a PreparedStatement as shown below:
java.util.Date jStartDate = ...; //your "java" date object
java.sql.Date startDate = new java.sql.Date(jStartDate.getTime());
java.util.Date jEndDate = ...;
java.sql.Date endDate = new java.sql.Date(jEndDate.getTime());
PreparedStatement p = connection.prepareStatement("select id from period where systemDate between ? and ?");
p.setDate(1, startDate);
p.setDate(2, endDate);
ResultSet rs = p.executeQuery();

Java has date formatting classes. DateFormat is the abstract parent class for these, SimpleDateFormat here is probably the one you want
SimpleDateFormat oracleStyle = new SimpleDateFormat("dd-MM-yy");
String dString = oracleStyle.format(systemDate);
String sql="select id from period where '"+systemDate+"' between startDate and endDate";
I havent tested this. The format call might need new string buffers as arguments, and my format string could be off, but this is the idea. Read the documentation

Related

How to get TimeZone offset value in java

I have a date coming from a device and I want to do the following in Java:
I want to parse this date "2021-05-27T18:47:07+0530" to yyyy-mm-dd HH:MM:SS
Get the Date's offset value so that I can get the timezone offset as +05:30 or whatever timezone it comes from.
For the first one I have done this and looks like it works, but any better smaller approach will be handy as well:
String date = "2021-05-27T18:47:07+0530";
String inputPattern = "yyyy-MM-dd'T'HH:mm:ss+SSSS";
String outputPattern = "yyyy-MM-dd HH:mm:ss";
LocalDateTime inputDate = null;
String outputDate = null;
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputPattern, Locale.ENGLISH);
inputDate = LocalDateTime.parse(date, inputFormatter);
outputDate = outputFormatter.format(inputDate);
System.out.println("inputDate: " + inputDate);
System.out.println("outputDate: " + outputDate);
The Ouput is:
inputDate: 2021-05-27T18:47:07.053
outputDate: 2021-05-27 18:47:07
I don't know how to get the offset value of timezone in this case.
There are many recommendations including using SimpleDateFormat and ZonedDateTime etc but should be the best answer for this considering the offset value can be dynamic i.e it can be +05:30,+09:00 etc.
Please help in this case.
Try it like this.
String dateTime = "2021-05-27T18:47:07+0530";
String inputPattern = "yyyy-MM-dd'T'HH:mm:ssZ";
String outputPattern = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ",
Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(dateTime, dtf);
ZoneOffset tz = zdt.getOffset();
System.out.println(tz);
System.out.println(zdt.format(DateTimeFormatter.ofPattern(outputPattern,
Locale.ENGLISH)));
Prints
+05:30
2021-05-27 18:47:07
The zoned date time could also be reprinted using the same format by which it was originally parsed.

Database Oracle, Query Date String JPA Repository Crud Repository, Java 8

NOTE: I can't put some privated code (Database or Java Code).
I have in the Database
CREATE TABLE "SCHEMA"."ENTITY"
(
"HCODFECREGISTR" DATE,
... BLABLA
)
The Entity
import java.util.Date;
public class Entity implements java.io.Serializable {
private Date hcodfecregistr;
....
}
In the Repository Interface
using
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface EntityRepository extends CrudRepository<Entity, Long>,
JpaRepository<Entity, Long> {
public static final String USING_STRING
= " SELECT enti FROM Entity enti WHERE "
+ " (enti.hcodfecregistr BETWEEN TO_DATE(:stringIni,'dd/MM/yyyy hh24:mi') AND TO_DATE(:stringEnd,'dd/MM/yyyy hh24:mi'))";
#Query(value = USING_STRING)
List<Object[]> getEntityUsingString(
#Param("stringIni") String stringIni, #Param("stringEnd") String stringEnd);
public static final String USING_DATE
= " SELECT enti FROM Entity enti WHERE "
+ " (enti.hcodfecregistr BETWEEN :dateIni AND :dateIni) ";
#Query(value = USING_DATE)
List<Object[]> getEntityUsingDate(
#Param("dateIni") Date dateIni, #Param("dateEnd") Date dateEnd);
}
Now I want to perform a query.
Date fecha = //some java.util.Date
Calendar calendar = Calendar.getInstance();
calendar.setTime(fecha);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date dateIni = calendar.getTime();
calendar.set(Calendar.HOUR, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
Date dateEnd = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String stringIni = dateFormat.format(fecha) + " 00:00";
String stringEnd = dateFormat.format(fecha) + " 23:59";
Works!
List<Object[]> listaObjects = theRepository.getEntityUsingString(stringIni, stringEnd);
Fails! It does not bring me results (but it also does not show any error).
List<Object[]> listaObjects = theRepository.getEntityUsingDate(dateIni, dateEnd);
Question:
I want to understand Why using the same fecha (java.util.Date) the method getEntityUsingString works, but using the method getEntityUsingDate fails (
When I set the range using Date it does not bring me results, whereas when I set the range with String it does.).
In my opinion, it should yield the same result that complies with the range of dates.
What is the problem?
You did not write what error it was, so it's only my guess that the most likely error in this case is:
ORA-01861: literal does not match format string.
If this is the case, then the reason is that you are passing to the query two values of date type:
Date dateIni = calendar.getTime();
.....
Date dateEnd = calendar.getTime();
.....
theRepository.getEntityUsingDate(dateIni, dateEnd);
these values are bind to this parameters :stringIni and :stringEnd
BETWEEN TO_DATE(:stringIni,'dd/MM/yyyy hh24:mi') AND TO_DATE(:stringEnd,'dd/MM/yyyy hh24:mi')
Since TO_DATE( char, [format] ) functions expects CHAR (string) as a first parameter, but gets a date, then according to rules explained in this documentation see section: implicit conversion
The following rules govern implicit data type conversions: .... ....
When you use a SQL function or operator with an argument of a data
type other than the one it accepts, Oracle converts the argument to
the accepted data type.
it does an implicit conversion from date to string using TO_CHAR function.
This function uses the default date language from a session to format strings, so depending of this settings the date may be converted to something like 12/15/54, 19-06-11 12:23 PM, JUN 23, 2019 13:20 AM +01 etc.
This string 12/15/54 is then passed to TO_DATE(:stringIni,'dd/MM/yyyy hh24:mi'), and because this sting doesnt match format string, the error is thrown: ORA-01861: literal does not match format string

Fetch date and time in xamarin forms

I have to fetch hours and minutes from time format I am getting from the system however it is showing me error like int month= moment.Month;
DateTime productDate = DateTime.Now;
string twentyFourHourFormatHour =int.Parse(productDate.ToString("HH")).ToString();
Not picking up productdate when trying to store it in string (line 2)
DateTime has properties to expose all of the data you need. There is no need to do elaborate string manipulation.
DateTime date = DateTime.Now();
int hour = date.Hour;
int minute = date.Minute;

Assigning a DateTime to a Date database column

I have error:
Cannot implicitly convert type 'string' to 'System.DateTime'a
When I want get a DateTime
var dat = DateTime.Now.ToShortDateString();
godz.date = dat;
godz.data is type Date in database.
How fix it?
That's because you are trying to pass a string where a datetime is expected. Try changing to this:
var dat = DateTime.Now;
How about setting
godz.date = DateTime.Now;

Parameter Date Conversion

am using C#, VS 2005 and SQL 2000
I have date conversion problem my SQL table having field as below
Fields:
datefrom
dateto
My SQL query in C# is
string sql = "insert into companymast (cname, datefrom, dateto) values(#cname, #datefrom, #dateto)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#cname",SqlDbType.Varchar(50)).Values = cname.text;
cmd.Parameters.Add("#datefrom",SqlDbType.Datetime).Values = maskedTextBox1.text;
cmd.Parameters.Add("#dateto",SqlDbType.Datetime).Values = maskedTextBox2.text;
cmd.ExecuteNonQuery();
But the above throw Error Like date non conversion string to date
I have to put date in dd/MM/yyyy format code, 103
so how do i do?
Help me out, please.
maskedBox1.text is a string. You're assigning that to a SqlParameter's data member, and it tries to convert whatever it receives to the data type you request.
A more solid solution would be to convert to DateTime explicitly, and pass the converted value to the SqlParameter. This way, you have more control over the conversion, and you'll see exactly where and why it goes wrong when it does. Example:
DateTime fromDate = DateTime.Parse(maskedTextBox1.text);
cmd.Parameters.Add("#datefrom",SqlDbType.Datetime).Values= fromDate;
For DateTime.Parse, see: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

Resources