Error converting parsing TIME dd.MM.yyyy', '12:00 - java

I have time on device 11:34
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'hh:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");// I PARSE THIS DATE!!!
RESULT IS :
date_start:
Wed Mar 12 00:00:00 Восточноевропейское время 2014
BUT SHOULD BE:
Wed Mar 12 12:00:00 Восточноевропейское время 2014
HOW to solve it?

To get 24h format use HH not hh. In 12h format hours can be in rage 0-11, which makes 12 overflow to 0.
Use
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");

use 24 hour date format
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");

First of all take a look about patterns of Simpledatrformat. where it clearly shows H is for (0-23).
Reference for Date Format Pattern Syntax
so you should change your code like below.
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");
System.out.println("now time is.." + date_start);
OR
Use this:
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
Thanks.. use above code to parse correctly!!

Related

How to convert /Date(1426066399983)/ to 23:00 Wed Mar 2015 in android

i have sent the sentTime of message in sql datable like
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm", Locale.getDefault()).format(new Date());
but when i am getting the sendTime from table it is like /Date(1426066399983)/
so that I want to convert string timestamp like /Date(1426066399983)/ to 23:00 Wed Mar 2015 format how can i do this
I am guessing that 1426066399983 is the milliseconds time? If so something like this will do using the Calendar:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1426066399983);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy/HH/mm");
String strdate = sdf.format(calendar.getTime());
Something like that should work
I'm supposing that your date number is a long.
long a = 1426066399983;
Date date = new Date((long) a);
String formattedDate = new SimpleDateFormat("dd/MM/yyyy - HH/mm").format(date);

GMT in java, simple but not working

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,

Java Date format weird

I have an issue where I would like to get the start of the day, however it seems to be setting it to 12:00 via automatic.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Why is this spitting out:
2014-06-06 12:00:00
The 12:00:00 is the issue
That is because hh represents the hour in 12 hour format. You need to use HH instead.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Have a look at the docs for more info.
Also, on a side note, there is a typo in your code.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date todayDate = dfFull.parse("2014-06-06 00:00:00"); // todayDate must be of type Date and not String
String today = dfFull.format(todayDate); // today should be of type String
System.out.println(today);
You should use HH for hours in this case.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Now you will get the out put as
2014-06-06 00:00:00
And again if you use hh that mean you are using 12 hour format while HH means 24 hour format
So

error parsing date

String dateString="2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
i get output as follows:
Converted string to date : Tue Jan 09 00:03:00 IST 2001
What is wrong with my code?
Use MM instead of mm for months - mm means minutes, not months.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
See the API documentation of SimpleDateFormat.

Java SimpleDateFormat Seemingly Ignoring Month and Day

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

Categories

Resources