I have used following line code to display time in 24-Hour format from the Calendar Instance
Everything show correctly, but only problem is while showing time at midnight 12:00 am, It show time as 24:00 instead of showing 00:00. Why this happen anything wrong in my code.
Calendar m_CalInstance = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("kk:mm");
String timeDisplay = formatter.format(m_CalInstance.getTime());
SimpleDateFormat formatter = new SimpleDateFormat("kk:mm");
should be
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
Check this javadoc for more information on SimpleDateFormat
use capital letters KK inplace of kk here
SimpleDateFormat formatter = new SimpleDateFormat("KK:mm");
Related
I am given an input date string for ex:2015-06-02 12:60:30 and the output should be 2015-06-02 00:00:00 i.e how to set the HH:mm:ss to zero in the given format yyyy-MM-dd HH:mm:ss ?
use yyyy-MM-dd 00:00:00 format instead of yyyy-MM-dd HH:mm:ss
it will change the hours, minutes and seconds to zero instead of actual values
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String dateValue = dateFormat.format(new Date());
System.out.println(dateValue);
You can try to use this:
calendar.set(Calendar.HOUR_OF_DAY, 0);
Something like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
System.out.println(sdf.format(c.getTime()));
c.set(Calendar.HOUR_OF_DAY, 0);
System.out.println(sdf.format(c.getTime()));
Simply provide a format for the portion of the "date" you want to keep, for example...
String text = "2015-06-02 12:60:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(text);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(out.format(date));
Outputs...
2015-06-02 00:00:00
This is a little trick, which is actually described in the JavaDocs
Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.
Emphasis added by me
try this
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
String strDate = sm.format(myDate);
If you don't require validation of the input format, you could use a regular expression:
input.replaceAll("\d\d:\d\d:\d\d", "00:00:00")
However, note that this conversion is not necessarily one which yields a valid time: midnight might not be valid, depending upon the date you are converting and its time zone, so this might not yield a valid time. (The start of daylight savings time in Asia/Gaza is the oft-cited example).
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.MONTH,6);
cal.set(Calendar.HOUR_OF_DAY,2);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
Date d = cal.getTime();
And to format it you can use:
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatted = sdFormat.format(cal.getTime());
I'm pretty new to java and am trying to format a time using 24 hour format. I've come across two ways of formatting the hour - HH and kk:
SimpleDateFormat format1 new SimpleDateFormat("HH:mm");
SimpleDateFormat format2 new SimpleDateFormat("kk:mm");
Date date = new Date();
System.out.println(format1.format(date));
System.out.println(format2.format(date));
These both produce something like 11:21. What's the difference between them? Am I missing something?
The two formats essentially do the same thing but differ in how they handle midnight. kk will format midnight to 24:00 whereas HH will format to 00:00. The hours in a day in k are 1-24 and in H are 0-23
It's always worth checking the java documentation as it generally provides very useful explanations as well as examples of uses.
try this to see the difference
SimpleDateFormat format1 = new SimpleDateFormat("HH:mm");
SimpleDateFormat format2 = new SimpleDateFormat("kk:mm");
Date date = new GregorianCalendar(2001, 0, 1, 0, 0 , 0 ).getTime();
System.out.println(format1.format(date));
System.out.println(format2.format(date));
output
00:00
24:00
For getting Current date in mm/dd/yyyy format I am using the below code
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
Date date = new Date();
String date3= sdf.format(date);
date = sdf.parse(date3);
but everytime I print the date ,it gives me wrong and random output
output
Currentd Date:: 49/22/2013
output
Currentd Date:: 07/22/2013
Kindly suggest as what I should use to get current date.
The Java Version I am using is 1.4
Change "mm/dd/yyyy" into "MM/dd/yyyy". m(lowercase) is use for minutes not for month. For month you should use M(uppercase)
You might want to use MM instead of mm in the format pattern which will give you month instead of minutes.
Use MM/dd/yyyy
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
MM - Month
mm - Minute
m = Minute
M = Month
Thus you have to use "MM/dd/yyyy"
Try
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println(sdf.format(date));
Try
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22OR
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime()); //2014/08/06 16:00:22
I am using following Java code to get current date and time:
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
System.out.println(timeFormat.format(date));
The Output Time is always 2 hours ahead of my system time or my Time Zone. For example if above code outputs following time:
11:44:43
Then my system's time is:
09:44:43
Please help me out to solve this problem!
Try this:
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
TimeZone tz = TimeZone.getDefault();
Calendar c = Calendar.getInstance(tz);
Date date = c.getTime();
System.out.println(timeFormat.format(date));
If it's not working, try to change the time zone. Take a look at TimeZone.getTimeZone(String id).
your problem is Timezone
for exp.
this code for android.
change timezone of app.
because android default timezone is "UTC"
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
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.