DateTimePicker's format in java - java

I am using <sx:datetimepicker name="date1" label="From" displayFormat="yyyy-MM-dd"/> for picking the date. How ever I have to compare the picked date with date in mySQl table. The problem here is, the date in mySQl table is like 2014-09-29 20:36:25 and the format of piked date from struts2 tag is like Wed Dec 31 00:00:00 IST 2014. I am unable to compare both. Can any one tell me how to compare both? Or if I can change the format of picked time in java, how to do that?

You need to parse both Date to some same format so that you can compare that ..
see below code that can help you
public class Test {
public static void main(String[] args) {
SimpleDateFormat sdfForSturtsDate = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
SimpleDateFormat sdfForMySqlDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String DateFromSturts = "Wed Dec 31 00:00:00 IST 2014";
String DateFromMysql = "2014-09-29 20:36:25";
java.util.Date dateFromsturts= new java.util.Date();
java.util.Date dateFromMySql= new java.util.Date();
try {
dateFromsturts = sdfForSturtsDate.parse(DateFromSturts);
dateFromMySql = sdfForMySqlDate.parse(DateFromMysql);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// now you can compare dateFromsturts and dateFromMySql
}
}

Related

(Android) How to convert a time raw string (with timezone) to another string format with the same timezone

I have a time raw string, "2016-05-15T12:42:00.000-04:00" and I want to convert the string to "Wed 15 May 2016 12:42", which keeps the same timezone (-04:00) as its original source.
I have tried SimpleDateFormat but using it returns different timezones that are not the same as the timezone in my original string. Please help me achieve this in Android Studio!
Other examples:
2016-05-15T15:42:00.000-08:00 -> Wed 15 May 2016 15:42
2016-05-15T14:44:00.000-01:00 -> Wed 15 May 2016 14:44
public static String formatDateString(String originDateString) {
//Original format 2016-05-15T12:42:00.000-04:00
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat resultFormat = new SimpleDateFormat("EEE dd MMM yyyy HH:mm");
String dateString = "";
try {
Date date = originalFormat.parse(originDateString);
dateString = resultFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//returned format: Tue 14 May 2016 12:42
return dateString;
}
try saving it first without the TZ information
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

How to Convert String in format yyyy-MM-dd to Date in same format in Java [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 7 years ago.
I want to convert my String in format 2015-09-07 to Date in format 2015-09-07. While parsing the Date is getting in different format. I need the result Date in same format what the String is.
Thanks&Regards
Sony K Koshy
Try like this":
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = "2015-09-07";
Date dt = sdf.parse(str);
System.out.println(dt);
Also refer: SimpleDateFormat for details.
Also you can create a generic function like this
private Date parseStringToDate(String dt, String format) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dt);
}
Using SimpleDateFormat:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = "2015-09-07";
Date date = simpleDateFormat .parse(dateString);
System.out.println(date); //Mon Sep 07 00:00:00 CEST 2015
public class StringToDate {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("y-MM-dd");
String stringDate = "2015-09-07";
try {
Date date = dateFormat.parse(stringDate);
System.out.println(date); // it will display Mon Sep 07 00:00:00 IST 2015
System.out.println(dateFormat.format(date)); // it will display 2015-09-07
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

How to convert string like "Tue, 06 May 2014 15:27:23 +0000" to Java-Date?

I need to convert a string (e.g. "Tue, 06 May 2014 15:27:23 +0000") to java date to store in in a sqllite-database. I used the following code, but it does not work (ParseException):
public static Date formatDate(String theDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = null;
try {
date = formatter.parse(theDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Do you have any ideas? Thanks for your help!
Best regards,
Robin
You are not using the correct format.
Try:
EEE, d MMM yyyy HH:mm:ss Z

Exception in parsing date format [duplicate]

This question already has answers here:
Date Format JAVA
(5 answers)
Closed 9 years ago.
I have a date in the following format
//input date
Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)
//output date format
I want to change this to "dd-mm-yyyy, hh:mm:ss".
I get the input date format from db. I have to change that into output date format which i will be showing it in a grid.
I tried the following code.
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
try
{
Date date = outputDate.parse(facade.getDate.toString()); **//getting exception here**
outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
Date date1 = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").parse(outputDate
.format(date));
facade.setDate(date1);
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting
java.text.ParseException: Unparseable date: "2013-06-06 00:00:00.0".
Any help..
"2013-06-06 00:00:00.0" does not match "dd-mm-yyyy, hh:mm:ss" your format should be "dd-MM-yyyy hh:mm:ss" instead
But, looking at your code I'm guessing facade.getDate is actually a java.sql.Timestamp which inherits from java.util.Date so you can directly pass it to the format like so
new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(facade.getDate)
Here's some code which works for me:
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
String input = "Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time)";
DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z",
Locale.ENGLISH);
Date date = inputFormat.parse(input);
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",
Locale.ENGLISH);
outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String output = outputFormat.format(date);
System.out.println(output);
}
}
Things to consider:
You need to work out your output time zone. Currently I've got it set to UTC, but that may not be what you want.
You really need to take a step back and think things through. You've clearly got two different formats - you're trying to convert from one to the other. So creating three different SimpleDateFormat objects all with the same format is never going to work.
You need to read documentation carefully... in SimpleDateFormat, M means month and m means minute; h uses the 12-hour clock and H uses the 24-hour clock.
This is assuming you actually need to start with a string though. If getDate is already a Date or a Timestamp, you can ignore the first part - just use the output part of the above code. You should avoid unnecessary string conversions wherever possible.
Note that dd-MM-yyyy is a slightly unusual format - are you sure you don't actually want yyyy-MM-dd which is more common (and sortable)?
DateFormat outputDate = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss");
try {
Date date = outputDate.parse("2013-06-06 00:00:00.0");
System.out.println(new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
works well, line 1 was incorrect. Your SimpleDateFormat.parse needs to be in the exact format of the input date. Then you want to output it in a different format so you make another one and set the format then call SimpleDateFormat.format(date) and I put a println on it.
Fault is here
DateFormat outputDate = new SimpleDateFormat("dd-mm-yyyy, hh:mm:ss");
pattern should be equals to Thu Jun 06 2013 00:00:00 GMT+0530 (India Standard Time). not to your out put strings pattern.
#Test
public void test() throws ParseException {
SimpleDateFormat sdf_org = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
Date d = sdf_org.parse("Thu Jun 06 2013 00:00:00 GMT+0530");
SimpleDateFormat sdf_target = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
System.out.println(sdf_target.format(d));
}
output console : 2013-30-06 03:30:00.000

Changing one date format to another

I 'm getting the date in the following format (Date and not String):
Tue Jun 26 07:00:00 EDT 2012
I want to change the format of the date to (Date):
6/26/2012 10:19:15 AM
so as to update the same in the data base. I tried following code:
Date dte;
Date dte1;(Tue Jun 26 07:00:00 EDT 2012)
SimpleDateFormat formatter = new SimpleDateFormat("m/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte1);
dte = formatter.parse(formattedDate);
SystemUtils.trace("test", " date>>>" + dte);
is yielding the following response:
Thu Jan 26 07:00:00 EST 2012
Can any one please share the piece of code to do the same asap.
You shouldn't have to format dates to insert them in a database. If using JDBC, use prepared statements.
To answer your question, though, m can't mean minute and month at the same time. M means month. m means minute.
This code outputs needed for you result:
Date dte = new Date();//or something else
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte);
System.out.println(formattedDate);
Try this, this below code will suit your need.
public class DateWala {
public static void main(String[] args){
System.out.println(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").format(new Date()));
}
}
you can use this tiny function
// send your time
private String convertTime(String dateTime) {
//source format will go there
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date date = null;
try {
date = sdfSource.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
//destination format will go there
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy HH:mm");
return sdfDestination.format(date);
}

Categories