I am trying to do an integration between 2 system,
so there is a datetime field in the source system which has format like
"dd-MMM-yyyy HH:mm:ss" for example- 19-JUN-2017 16:12:30
but what my target system reads is like "yyyy-MM-DD HH:mm:ss"
for example- 2017-12-04 19:14:16
As I am setting a value like
Tableset.setValue("TRANSACTIONDATE",CSVFILECOLUMN[1], 2L);
can any one tell me how can I parse the date format in CSV file here to set the parsed value in my TRANSACTIONDATE field.
Covert your date like this
String mydate="19-JUN-2017 16:12:30";
SimpleDateFormat currentFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
Date d = currentFormat.parse(mydate);
String formattedTime = newFormat.format(d);
then calculate difference
Related
Before to post, i search this format in previous questions but i dont found it
Here is the simple code that i want to try :
String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:ss";
DateTimeFormatter yyyyMMddHHmmssFormatter = DateTimeFormat.forPattern(YYYYMMDD_HHMMSS);
DateTime fromDate = DateTime.parse("2019-01-17 11:01:15", yyyyMMddHHmmssFormatter);
I got this error :
java.lang.IllegalArgumentException: Invalid format: "2019-01-17 11:01:15" is malformed at "-01-17 11:01:15"
I think, i must change something in the string format ?
Thank's for your help
You shoud use format yyyy-MM-dd HH:mm:ss for input 2019-01-17 11:01:15, yyyy/MM/dd HH:mm:ss works when the input like 2019/01/17 11:01:15.
Update
You need two format to convet:
DateTimeFormatter inputFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter outputFormat = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
DateTime fromDate = DateTime.parse("2019-01-17 11:01:15", inputFormat);
System.out.println(outputFormat.print(fromDate));
Try changing
String YYYYMMDD_HHMMSS = "yyyy/MM/dd HH:mm:ss";
to
String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
I'd like to calculate the time difference between two dates, but I'm unable to parse a string representation of a date that looks like 2015-01-13 15:59:10, for example, through a formatter SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Only string dates in the form, String dateStart = "01/14/2012 09:29:58";, for example work.
How should I go about parsing 2015-01-13 15:59:10, for example through SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");?
Your format needs to match the literal you're trying to parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
instead of
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Your format must match your input string. The correct format is:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
See the documentation for more details and an explanation.
to parse 2015-01-13 15:59:10 you should change the date format to below:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
I have a string containing date with timezone like
2014-01-24 09.05.14 -06:00.
I need to convert this into date format as YYYYMMDD.
What is the best way in Java to convert the above string into date.
Do it like this (requires JDK 7+):
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss X");
DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = "2014-01-24 09.05.14 -06:00";
Date date = inputFormat.parse(dateString);
System.out.println(outputFormat.format(date));
One way of doing it is to convert the String into a Date and then use a DateFormatter (e.g. the SimpleDateFormat) to formate that date.
Take a look at this Tutorial for further information.
I have been developing application with GWT on Java. I used calendar tool for my project on this site. When i select any date, give this format (23-Aug-13) to me.
I use this code:
final DateField txtBirthofDay = new DateField("Birth of Day", "dob", 190);
txtBirthofDay.setValue(new Date());
I want to insert this date to database. So, I have to convert this format (2013-08-23). I converted lots of date format each other before but I didn't convert string type (Aug).
Reference these formats Java Date Format Docs:
try this:
SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy");
Date date = dt.parse("23-Aug-2013");;
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dt1.format(date));
SimpleDateFormat formats your Date to the given pattern
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(new Date()));
Try this,
DateFormat df=new SimpleDateFormat("dd-MMM-yy");
Date date=df.parse("23-Aug-13");
System.out.println(df.format(date));
df=new SimpleDateFormat("yyyy-MM-dd");
String requiredFormatedDate=df.format(date);
System.out.println(requiredFormatedDate);// 2013-08-23
I have below code snippet
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS");
String processedContentDate="2012-04-10 12:53:28.033";
java.util.Date parsedDate = dateFormat.parse(processedContentDate);
java.sql.Timestamp timestamp = new java.sql.Timestamp(
parsedDate.getTime());
I get parsed date as Tue Apr 10 00:53:28 IST 2012 and timestamp as 2012-04-10 00:53:28.033 . i want to get the time exactly as 12:53:28.033(as in my original string)
not 00:53:28.033. Not getting why 12:53:28 is getting converted to 00:53:28. what should I do to get 12:53:28?
EDIT: After getting the response, I tried this small programme where current time is 14:34:38.899
but at both lines i.e at line 1 and line 2, I got below parsed date
2012-04-10 14:34:38.899
As per reply I should have got 02:34:38.899 at line 1 as date format is yyyy-MM-dd hh:mm:ss.SSS")
java.util.Date date= new java.util.Date();
String strDate=date.toString();
java.util.Date parsedDate;
java.util.Date parsedDate2;
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS");// line 1
SimpleDateFormat dateFormat2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");//line 2
try {
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
strDate=timestamp.toString();
parsedDate = dateFormat.parse(strDate);//line1
parsedDate2 = dateFormat2.parse(strDate);//line2
Define your dateFormat like that
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
HH instead of hh. See SimpleDateFormat
Your date format must be yyyy-MM-dd HH:mm:ss.SSS.
hh is hours in am/pm, while HH is hours in a day (that's where you mistake is). See SimpleDateFormat.
As per definition of Date.toString() and Timestamp.toString, the .toString() output is always using a 24-hour clock. If you want to show the time using AM/PM, you should use the dateformatter to print the date. As you are using the same date/time as a source for both (strDate will use 14:34), when you parse the date, the SimpleDateFormat using the 12-hour clock is "lenient" and allows parsing of 14 as an hour.
If you set
dateFormat.setLenient(false);
you'll probably find that the dateFormat.parse(strDate) will fail.
To print dates, I would never rely on toString, but always use a formatter.
System.out.println(dateFormat.format(parsedDate)); // should show ...02:36...
System.out.println(dateFormat2.format(parsedDate)); // should show ...14:36...
System.out.println(dateFormat.format(parsedDate2)); // should show ...02:36...
System.out.println(dateFormat2.format(parsedDate2)); // should show ...14:36...
Try below code:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = fmt.parse("yourdate");
SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy hh:mm a");String myDate = fmtOut.format(date);
If yourdate is 2016-06-10 12:06:43, then output will be 10-06-2016 12:06 pm.