This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to parse date in java?
I want to convert the string "11-10-10 12:00:00" into a Date object, but I am not able to do so. Can you please help me out?
I have the Date object which has the value "Mon Oct 11 00:00:00 IST 2010"
DateFormat newDateFormat = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
String strDate = newDateFormat.format(tempDate);
//**i got strDate as strDate is : 11-10-10 12:00:00**
DateFormat newDateFormat1 = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
try {
tempDate = newDateFormat1.parse(strDate);
// **getting tempDate as - Mon Oct 11 00:00:00 IST 2010**
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateFormat newDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
Date d = newDateFormat.parse("11-10-10 12:00:00");
System.out.println(d);
Here is ideone demo
I think this is Java code—not my specialty—but I think your issue is the "hh" in your format string, which causes parse to interpret "12:00:00" as midnight instead of noon. Try changing it to "HH" and see if that parses correctly.
You need to use CultureInfo for Hindi it is "hi-IN". For full list of cultures check this link CultureInfo
class Program
{
static void Main(string[] args)
{
var str = "11-10-10 12:00:00";
DateTime dateTime = DateTime.Parse(str, new CultureInfo("hi-IN", false));
}
}
Related
This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29
The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".
SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour
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();
}
}
}
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
}
}
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
This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29
The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".
SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour