I get this date as a string from SOAP message
"2009-12-02T12:58:38.415+01:00"
Most i could i could identify and vary on subject was
to play with this format yyyy-MM-dd ? hh:mm:ss.????
I tried different combinations using SSS T z Z instead of '?"
DateFormat df = new SimpleDateFormat("... various formats ...");
System.out.println(df.parse("2009-12-02T12:58:38.415+01:00"));
but no success.
Any idea ?
Thanks
you have to change the timezone part. try this:
String a = "2009-12-02T12:58:38.415+01:00";
a = a.replaceFirst(":(?=\\d+$)", "");
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(df.parse(a));
I don't think this is possible using the standard Java date formatting.
Using joda-time this can be done using the following code:
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder()
.appendYear(4, 4).appendLiteral('-').appendMonthOfYear(2).appendLiteral('-').appendDayOfMonth(2)
.appendLiteral('T')
.appendHourOfDay(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':').appendSecondOfMinute(2)
.appendLiteral('.').appendMillisOfSecond(3).appendTimeZoneOffset(null, true, 2, 2);
DateTimeFormatter dateTimeParser = b.toFormatter();
System.out.println(dateTimeParser.parseDateTime("2009-12-02T12:58:38.415+01:00"));
Try with the following.
String date = "2009-12-02T12:58:38.415+01:00";
int lastIndexOf = date.lastIndexOf(":");
if(lastIndexOf>=0){
date = date.substring(0,lastIndexOf)+date.substring(lastIndexOf+1);
}
System.out.println("~~~~~~date~~~~~"+date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSZ");
try {
sdf.parse(date);
System.out.println("....date..."+date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related
I'm trying to convert a date (string) extracted from a csv file, convert it to sql timestamp and upload using prepared statement. What I have is:
String test = "8/10/2014 16:59";
DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
fromFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
toFormat.setLenient(false);
Date date2 = null;
try {
date2 = toFormat.parse(test);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf(date2);
//java.sql.Timestamp sqlDate2 = new java.sql.Timestamp(timestamp);
//sql_statement.setTimestamp(1, ts2);
As you can see my code is messy as I'm trying to solve this problem. I'm always getting an error in eclipse:
java.text.ParseException: Unparseable date: "8/10/2014 16:59"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.syntronic.client.thread.ORCThreadTejasInv.uploadOracleDBOptical(ORCThreadTejasInv.java:555)
at com.syntronic.client.thread.ORCThreadTejasInv.connectOracleDB(ORCThreadTejasInv.java:170)
at com.syntronic.client.thread.ORCThreadTejasInv.retrieveOracleTejas(ORCThreadTejasInv.java:125)
at com.syntronic.client.thread.ORCThreadTejasInv.run(ORCThreadTejasInv.java:84)
at java.lang.Thread.run(Thread.java:745)
I even try using:
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
String yourformattedDate = sdf.format(test);
and diff error shows up"
Exception in thread "Thread-6" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:301)
at java.text.Format.format(Format.java:157)
at com.syntronic.client.thread.ORCThreadTejasInv.uploadOracleDBOptical(ORCThreadTejasInv.java:562)
at com.syntronic.client.thread.ORCThreadTejasInv.connectOracleDB(ORCThreadTejasInv.java:170)
at com.syntronic.client.thread.ORCThreadTejasInv.retrieveOracleTejas(ORCThreadTejasInv.java:125)
at com.syntronic.client.thread.ORCThreadTejasInv.run(ORCThreadTejasInv.java:84)
at java.lang.Thread.run(Thread.java:745)
Anyone can help on why the date is unparseable? and how to convert it to a proper sql timestamp? thank you
Your fromFormat format specifier is
dd/MM/yyyy hh:mm
but should be
dd/MM/yyyy HH:mm
And change the toFormat to yyyy-MM-dd HH:mm:ss.SSSSSS
Then your parse code should change from
date2 = toFormat.parse(test);
to
date2 = fromFormat.parse(test);
System.out.println(toFormat.format(date2));
And I get the output
2014-10-08 04:59:00.000000
Please use following code it will serve your need
String test = "8/10/2014 16:59";
Date date2 = null;
SimpleDateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
try
{
date2 = fromFormat.parse(test);
Timestamp tt = new Timestamp(date2.getTime());
System.out.println(tt);
} catch (ParseException ex)
{
date2 = null;
}
When you parse a date-string that looks like this:
String test = "8/10/2014 16:59";
you're using 24-hours format (16:59) you should use HH instead of hh.
See the following code snippet:
DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
fromFormat.setLenient(false);
Date date2 = null;
try {
date2 = fromFormat.parse(test);
System.out.println("date2 = " + date2); // prints date2 = Wed Oct 08 16:59:00 PDT 2014
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So you have a String, and you want to parse it as a Date... Let's see if this example helps you:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateConverter
{
public static void main(String[] args) {
String test = "8/10/2014 16:59";
SimpleDateFormat sdf1 = new SimpleDateFormat("d/MM/yyyy HH:mm"),
sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*
'sdf1' will be used to parse your input string as a date
'sdf2' will be used to output a string with your desired date format
*/
Date d;
String formatted_date;
try {
// Parse the string to a date, using the defined format
d = sdf1.parse(test);
// Now, format the date with 'sdf2' and store it in a string
formatted_date = sdf2.format(d);
System.out.println(formatted_date); // The output is: 2014-10-08 16:59:00
} catch(ParseException e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
}
Was able to fix it using the code below:
DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date2 = null;
String def = perRow[cnt].replaceAll("8", "08");
try {
date2 = fromFormat.parse(def);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long tsTime1 = date2.getTime();
java.sql.Timestamp sqlDate2 = new java.sql.Timestamp(tsTime1);
sql_statement.setTimestamp(2, sqlDate2);
Of course, I don't know if this is the correct or proer way to it as:
1. replace the sting with correct day 'dd'
2. parse to date format
3. convert to long
4. convert to sql date
Anyone knows a better way or idea, thread is open for comments. thank you.
You can convert it using java.sql.Timestamp. Here is a snippet:
String strDate = "15/07/1989 15:30";
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date date = format.parse(strDate);
System.out.println(date);
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
System.out.println(timestamp);
And the output will be:
Sat Jul 15 15:30:00 IST 1989
1989-07-15 15:30:00.0
Simple!
First parse fromDate then format in toDate pattern.
toFormat.format(fromFormat.parse(test));
I want to create a method to validate a date by using SimpleDateFormat.
If the date is valid(e.g. 02/09/2012 or 2/09/2012 or 02/9/2012), this method should return true.
But if the format of the date is wrong(e.g. 02/09/201X) or logically wrong(e.g. 32/09/2012), this method should return false.
I try to write this method like this:
private boolean isValidDate(String date) {
DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
DateFormat df2 = new SimpleDateFormat("d-MM-yyyy");
DateFormat df3 = new SimpleDateFormat("dd-M-yyyy");
Date d = null;
String s = null;
try {
d = df1.parse(date);
}catch (Exception e1) {
try{
d = df2.parse(date);
}catch (Exception e2) {
try {
d= df3.parse(date);
}catch (Exception e3) {
return false;
}
s = df3.format(d);
return date.equals(s);
}
s = df2.format(d);
return date.equals(s);
}
s = df1.format(d);
return date.equals(s);
}
But if I validate a date, for instance, 2/09/2012, it returns false (actually it should return true). I have no idea why... Can anyone find what's the problem with my code, or this logic is totally wrong? Is there any better way to do this validation?
Your input is in the format 2/09/2012 not 2-09-2012, so your dateformat should be like below:
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
DateFormat df2 = new SimpleDateFormat("d/MM/yyyy");
DateFormat df3 = new SimpleDateFormat("dd/M/yyyy");
I think your code is fine (but not very scalable - try to do it in a for-loop in case you add more formats later).
The problem is that your format strings are wrong. Instead of dd-MM-yyyy you should have dd/MM/yyyy. The same goes for the rest of the formats:
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
DateFormat df2 = new SimpleDateFormat("d/MM/yyyy");
DateFormat df3 = new SimpleDateFormat("dd/M/yyyy");
The validation fails because / isn't -.
Add this additional check after parsing each string:
Tokenize the value of the date String
Then use the extracted day, month and year values to create a new Date object using GregorianCalendar
Compare this to the date object you created from parsing the date strings
If they match then you know that the input string contained a valid date format
String myDate = new String("2011-06-23T00:00:00");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
this.thedate = format.parse(myDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm not sure what I'm doing, but I have a timestamp that will be a string and I want to parse out the year and month. This is what I have so far.
You need to use "yyyy-MM-dd'T'HH:mm:ss" as a pattern for the SimpleDateFormat:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
I guess format should be this:
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Hello I am using an android application and I am trying to figure out how to convert a 24 hour time to a 12hour time.
Example
24 hour format 12:18:00
to
12 hour format 12:18pm
Try using a SimpleDateFormat:
String s = "12:18:00";
DateFormat f1 = new SimpleDateFormat("HH:mm:ss"); //HH for hour of the day (0 - 23)
Date d = f1.parse(s);
DateFormat f2 = new SimpleDateFormat("h:mma");
f2.format(d).toLowerCase(); // "12:18am"
If you are using Java 8 or 9 you can use java.time library like this :
String time = "22:18:00";
String result = LocalTime.parse(time).format(DateTimeFormatter.ofPattern("h:mma"));
Output
10:18PM
You'll most likely need to take a look at Java SimpleDateFormat.
To display the data in the format you want you should use something like this:
SimpleDateFormat sdf=new SimpleDateFormat("h:mm a");
sdf.format(dateObject);
Use SimpleDateFormat but note that HH is different from hh.
Say we have a time of 18:20
The format below would return 18:20 PM
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm aa");
While this format would return 6:20 PM
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
Hope this helps...
final String timein24Format = "22:10";
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(timein24Format );
String timein12Format=new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
try this code
String s= time ;
DateFormat f1 = new SimpleDateFormat("kk:mm");
Date d = null;
try {
d = f1.parse(s);
DateFormat f2 = new SimpleDateFormat("h:mma");
time = f2.format(d).toUpperCase(); // "12:18am"
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have a date in(string) in dd-mon-yyyy format and I want to compare this date with system date.
eg.
I have 12-OCT-2010
and I want to compere this with system date in same format
You can use the SystemDateFormat class to parse your String, for example
final DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
final Date input = fmt.parse("12-OCT-2010");
if (input.before(new Date()) {
// do something
}
Note that SimpleDateFormat is not threadsafe, so needs to be wrapped in a ThreadLocal if you have more than one thread accessing your code.
You may also be interested in Joda, which provides a better date API
Use SimpleDateFormat http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
String d = "12-OCT-2010";
try {
Date formatted = f.parse(d);
Date sysDate = new Date();
System.out.println(formatted);
System.out.println(sysDate);
if(formatted.before(sysDate)){
System.out.println("Formatted Date is older");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I would recommend using Joda Time. You can parse that String into a LocalDate object very simply, and then construct another LocalDate from the system clock. You can then compare these dates.
Using simpledateformat -
String df = "dd-MMM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(df);
Calendar cal = Calendar.getInstance();
/* system date */
String systemdate = sdf.format(cal.getTime());
/* the date you want to compare in string format */
String yourdate = "12-Oct-2010";
Date ydate = null;
try {
ydate = sdf.parse(yourdate);
} catch (ParseException e) {
e.printStackTrace();
}
yourdate = sdf.format(ydate);
System.out.println(systemdate.equals(yourdate) ? "true" : "false");