I am parsing a date, where I am passing 2011-02-10 00:00:00:0, and I am getting 2011-01-10 as the result. Is this wrong? Please help.
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
today = df.parse(datecollection);
The pattern for month is MM: yyyy-MM-dd. You should read the API doc.
You should be using MM not mm. MM is for month, mm is for minute
try using MM instead of mm should work
refer http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
M Month in year Month July; Jul; 07
m Minute in hour Number 30
an example
"yyyy-MM-dd HH:mm:ss:SSS" 2001-07-04 12:08:56:235
You need to replace mm with MM. Please take a look at this page.
try
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.parse("2011-02-10 00:00:00:0"));
}
catch (Exception e){}
Prints:
Thu Feb 10 00:00:00 CET 2011
Related
This is both a duplicate and not a duplicate!
Just please help me and don't refer me to anywhere else, cause I'm really unable to get the GMT time.
The answer seems easy but it doesn't work for me.
I don't know are the answers all aver the web wrong, or am I making a mistake?
Please take a look at this snippet and the results:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Date date = new Date(time);
System.out.println(date);
time = System.currentTimeMillis();
date = new Date(time);
System.out.println("--\n" + date);
result :
Fri Feb 28 16:07:12 GMT+03:30 2014--
Fri Feb 28 16:07:12 GMT+03:30 2014
Both show my local time. I even printed directly the time, cause I thought maybe this is due to Date class but even those are the same (with just about 1 or 2 milliseconds difference).
Use setTimeZone() on a SimpleDateFormat to print the date in a specific timezone.
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
Date d = format.parse("28-Feb-2014 13:00:00 PST");
System.out.println(format.format(d));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(d));
Prints:
28-Feb-2014 13:00:00 PST
28-Feb-2014 21:00:00 GMT
Try the below
SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(f.format(new Date(System.currentTimeMillis())));
One way of doing it is using SimpleDateFormat and the setTimeZone method():
SimpleDateFormat sdf = new SimpleDateFormat( "..." );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // or UTC
System.out.println( sdf.format( date ) );
Cheers,
In my HTML part I have text box where the date is entered as: 10.09.2013 (dd.mm.yyyy)
What I'm doing in my servlet is:
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
String dateInString = invoiceData.getString("date");
Date date = formatter.parse(dateInString);
System.out.println(date);
The system prints: Thu Jan 10 00:09:00 EET 2013 and inside of the oracle database it's inserted as 10-SEP-2013
Please help me! I'm dealing with it for about 2 hours and I really can not find a way to convert it and insert it correctly in my database. I know that I'm missing something super small, but I'm unable to spot it.
I'm using Jboss 7.1 server.
new SimpleDateFormat("dd.MM.yyyy");
instead of
new SimpleDateFormat("dd.mm.yyyy");
mm stands for Minutes. MM stands for Month
Take a look date format
mm is used for minutes and MM is used for Month
SO you need to change:
new SimpleDateFormat("dd.MM.yyyy");
mm stands for minutes.
You need to use MM in your pattern to get the month.
mm for Minutes and MM for Month. Change the format to dd.MM.yyyy get the correct date.
Change your code like below:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
String dateInString = invoiceData.getString("date");
Date date = formatter.parse(dateInString);
System.out.println(date);
use following format
new SimpleDateFormat("dd.MM.yyyy");
I'm using SimpleDateFormat to parse Date String ase bellow:
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-DD");
System.out.println(formatter.parse(firstDate));
And for String "2011-04-02" i got after parsing "Sun Jan 02 00:00:00 EET 2011" so Month is Jan there but must be Apr.
What i'm doing wrong?
DD is day in year. You need dd (lowercase) instead, which is day in month.
See documentation of SimpleDateFormat for more info.
Try: SimpleDateFormat("yyyy-MM-dd").
DD is day of year, that's why you got 2nd of January.
try this
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.parse(firstDate));
Your date format incorrect. use "yyyy-MM-dd"
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.parse("2011-04-02"));
Live Demo
I would like the time respresented as this "Thu Jan 01 08:40:38 GMT+01:00 1754"
so i did this :
Date datu = new Date();
datu.setYear(1754);
datu.setMonth(0);
datu.setDate(1);
DateFormat.format("%tc", datu);
// DATU = Thu Jan 01 08:40:38 GMT+01:00 3654
String startTime = datu.toGMTString();
now the problem is that i set the year on 1754 in the code. but as I print it out. there is 3654 ?
EDIT:
datu.setYear(1754-1900) can do the trick. but isn't there another way?
Please try like this :
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mm:ss 'GMT'Z yyyy");
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,1754);
c.set(Calendar.MONTH, 0); // JAN
c.set(Calendar.DATE, 1);
System.out.println(sdf.format(new Date(c.getTimeInMillis())));
Output
Tue Jan 01 05:23:55 GMT+0800 1754
What version of the Java API are you using?
I would suggest you use the java.text.SimpleDateFormat and java.util.Calendar instead.
You code would look something like this:
Calendar cal = Calendar.getInstance();
cal.set(1754, 0, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // <- customize your format
sdf.format(cal.getTime()); // <- get your string
The date.toGMTString() method is also deprecated so it's best to avoid it.
It seems that date.SetYear is deprecated. According to the documentation you should use Calendar.set(Calendar.YEAR, year) instead.
As you said, date.setYear(desired year-1900) fixes the problem. It is noteworthy that the method is deprecated it is better you use the Calendar class which doesnt require subtracting 1900 from the year. A code snippet that will achieve what you are doing using Calendar is giving below:
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);// note that the date object here will be datu according to
//your code. It could be any object of the Date class
int year = calendar.get(Calendar.YEAR);
Hope this helps.
Feel free to mark as answer if it solves your problem.
Good luck!
I have this piece of simple code:
SimpleDateFormat sqlFormatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
I get this output:
2012-03-09 12:00:00
Sun Jan 01 12:00:00 EST 2012
I know is supposed to be simple, but I am hoping someone can quickly see what I am missing.
I think your pattern is a little off. I'm suprised you're not seeing an IllegalArgumentException. Try using the following pattern with lower case y's and see if that resolves your issue:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Corrected code here:
SimpleDateFormat sqlFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println(temp);
Date last = sqlFormatter.parse(temp);
System.out.println(last);
You should have SimpleDateFormat instead of SimpleDateFormatter and for years you give yyyy instead of YYYY.
Once I corrected your format String - Y is not allowed, you need y - (and the typo already mentioned) it worked fine:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
>2012-03-09 12:00:00
>Fri Mar 09 12:00:00 EST 2012
You need to use 'yyyy' and not 'YYYY'
Here is the output
2012-03-09 12:00:00
Fri Mar 09 12:00:00 IST 2012
for the pattern
yyyy-MM-dd HH:mm:ss