How to get Timestamp with AM/PM in java - java

I have a date as String , which needs to be converted in to Time Stamp with AM/PM . I tried the below way, I'm getting the proper date format but didn't get in AM/PM.
Can any one please help ?
code Snippet:
String dateString = "10/10/2010 11:23:29 AM";
SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
Date date = new Date();
date = sfdate.parse(dateString);
System.out.println(new Timestamp(date.getTime()));
Which gives me the output as below :
2010-10-10 11:23:29.0
But I needs it like this
2010-10-10 11:23:29.00000000 AM
Kindly help me please.

Why create a timestamp ? When you can just :
SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
Date date = new Date();
date = sfdate.parse(dateString);
System.out.println(sfdate.format(date) );
Output:
10/10/10 11:23:29 AM

Try:
System.out.println(sfdate.format(date));
As your last line rather than the one that you have at current.

Timestamp.toString() prints to a specific format: yyyy-mm-dd hh:mm:ss.fffffffff. The Timestamp object itself should be correct, if that's all you are looking for.
If you then want to define another format in order to print it as you like, that would require you to format Date object, using an appropriate pattern for the output format you are looking for.

What you're seeing is the result of Timestamp.toString(). The actual value in the Timestamp object instance is valid.
If you're getting an error in a subsequent SQL operation, please post that error along with the code you're using.

Related

How to print date string with ISO8601 format?

Please help me to print my date in below format
Example: 2020-08-05T16:17:10,777
I tried with below date converter but it is not giving the output that I want.
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
String text = sdf.format(requestTime);
loggdata.append(REQUEST_TIME + requestDate);
I got date printed like "2020-08-20T06:26:09.003763Z". Date is in UTC tomezone and format is different.
I can see many question and answers here in stackoverflow. But here in my case I need exactly this format 2020-08-05T16:17:10,777 see the last portion ",777".
Also I need to display the time in local timezone
Found a solution for the same. I have used "LocalDateTime" for the same.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss,SSS");
String dateInString= now.format(formatter);
It displayed date like this "2020-08-20T21:18:56,321"

Oracle Date Format change to yyyy-MM-dd hh:mm:ss

I have date saletime as 2/25/14 22:06 I want to store it in oracle table in the yyyy-MM-dd hh:mm:ss. So I wrote following java code
Date saleTime = sale.getSaleTime();
logger.info("DateTime is "+saleTime);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date saleTimeNorm = formatter.parse(formatter.format(saleTime));
logger.info("DateTime after Formating "+saleTimeNorm);
Timestamp oracleDate = new Timestamp(saleTimeNorm.getTime());
logger.info("New Format Inserting :"+oracleDate);
sale.setSaleTime(oracleDate);
But this seems to be giving :0014-02-25 22:06:00.0
Any suggestions ?
Your getSaleTime() method somehow regards "14" as a four-digit year, and returns the year 14.
After you have executed getSaleTime(), you already have a Date variable; there is no need (and no use) in converting it to a different output format and re-parsing the result. The Date you get from the calls to format() and parse() will be the same one you started with.
You can create your Timestamp using getTime() on the result of the call to getSaleTime(). That will be correct once you change getSaleTime() so that it returns the date in the correct year.
Something must be wrong in your sale.getSaleTime() method. Because the following code working as needed.
Date saleTime = Calendar.getInstance().getTime();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date saleTimeNorm = formatter.parse(formatter.format(saleTime));
Timestamp oracleDate = new Timestamp(saleTimeNorm.getTime());
System.out.println(oracleDate);
//2014-05-13 03:58:53.0

Converting string into date type

I have used the Calendar class to get the current Date. Now I want to convert that date to Date format so that it can be stored in database with format "yyyy.mm.dd". I tried to convert this using SimpleDateFormat class
String dateString = dateText.getText();
SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy.mm.dd ");
Date convertedDate = dateFormat.parse(dateString);
but I couldn't convert into Date type.
Try to remove spaces from the format string
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.mm.dd");
Also if your input date has invalid format you might get a parse exception. Better if you put it into try/catch block.
Notice, that m stands for minute in hour but M for month of year. Make sure you put a valid format pattern.
You havent stated what the error is but its unlikely that you want to use a minute field to parse the month. Use uppercase M:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
get rid of the whitespaces in your pattern

Getting formatted date in date format rather than String

I'm running the program written below, but instead of printing in mm/dd/yyyy hh:mm format it prints in the normal date format(ie. Day Date and time)
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy hh:mm");
Date date = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
The reason i'm doing this is because the existing method accepts parameters in Date format, so i need to send the above mentioned date object to it.
Please point out the mistake or suggest some other alternative.
Thanks
Date objects don't have a format. The Date class is a wrapper around a single long, the number of milliseconds since the epoch. You can't "format" a Date, only a String. Pass around a Date/Calendar internally, and format it whenever you need to display it, log it, or otherwise return it to the user.
Change the format to MM/dd/yyyy. Month is denoted by capital M.
Check below URL for valid formats
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your formatter works quite fine (apart from the mm vs. MM bug). You get a formatted string from the date and then create a copy from your date by parsing the formatted string:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
Date now = Calendar.getInstance().getTime();
String formattedNow = sdf.format(now); // == "09/24/2013 01:59"
Date now2 = sdf.parse(formattedNow); // == now

Jackrabbit dateformat for comparison

I am using Jackrabbit to store my documents.
Now I would like to search for documents that were created e.g. after a specific date using XPATH. To do so, I tried something like:
String dateString = date.toString();
//element(*,nt:file)[#jcr:created >= xs:dateTime(dateString)]
date is an object of class java.util.Date
dateString gets formatted as: Wed Mar 16 00:00:00 CET 2011
But this is giving me an InvalidQueryException, indicating that the dateString is wrong:
Invalid query: Lexical error at line
1, column 136. Encountered: "0" (48),
after : ":" for statement
So the question is: What is the correct format of a date for xs:dateTime ?
Thanks in advance
For Jackrabbit this worked for me:yyyy-MM-dd'T'HH:mm:ss.SSSX
(2015-12-16T15:16:50.465-02:00) when some previous code had taken a Calendar and done:prop.getValue().getString()
Couldn't get Z to work ("Unparseable date").
Just for the sake of completeness:
I found another (Jackrabbit/JCR dependend) way to get a correctly formatted date string:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String dateString = ValueFactoryImpl.getInstance().createValue(cal).getString();
This dateString can be used with the single arg constructor of xs:dateTime
xs:dateTime uses a specific pattern - see here and here. So instead of using date.toString(), to produce that format, you would need to use a suitable DateFormat. Something like this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String dateString = format.format(date);
However, it appears that the constructor for xs:dateTime in fact requires two args: one for date and one for time. See here.
So I would guess you could use this:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat tf = new SimpleDateFormat("HH:mm:ss");
String dateString = df.format(date);
String timeString = tf.format(date);
Also I have some problems with JAckRabbit date format and I needed to get some entities between two dates :
#createdDate >= xs:dateTime(startDate)
#createdDate <= xs:dateTime(endDate)
What I noticed is :
using format yyyy-MM-dd'T'HH:mm:ss.SSS'Z' to parse the date gave incorrect results( also it should be yyyy-MM-dd'T'HH:mm:ss.SSSZ) but you get for example :
2012-01-04T23:59:59.999+0200 instead of
2012-01-04T23:59:59.999+02:00 (saved in JCR)
Solution with ValueFactoryImpl.getInstance().createValue(cal).getString() works.

Categories

Resources