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"));
Related
static DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy HH:mma");
static DateTimeZone zone = DateTimeZone.forID("America/New_York");
static Chronology coptic = GJChronology.getInstance(zone);
static DateTime dt = new DateTime(coptic);
System.out.println("time is :::"+ dateFormat.format(dt.toDate()));
I am using the above piece of code to get a system time and it installed in websphere server.
Actually, its returning only server start up time not current time.Please guide me to get current time.
If you want the current date and time, you can use one of the following:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date));
or using Calendar.getInstance(),
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currentDate = dateFormat.format(cal.getTime());
Javadoc of GJChronology says:
Wherever possible, it is recommended to use the ISOChronology instead.
So you should just use:
DateTime dt = DateTime.now();
For printing the date/time, you should either lowercase the HH or remove the a, since 24-hour clock with AM/PM is wrong:
DateTimeFormatter dateFormat = DateTimeFormat.forPattern("MMM dd yyyy hh:mm a")
.withZone(DateTimeZone.forID("America/New_York"));
System.out.println("time is :::"+ dt.toString(dateFormat));
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 have date and time extracted from JSON in following format
2013-01-16T13:43:11
I need to convert it to local time of Pakistan and add 5 hours to that time so the result is like:
06:43
How I can achieve this?
Assuming that "2013-01-16T13:43:11" is in GMT
String s = "2013-01-16T13:43:11";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = df.parse(s);
date = new Date(date.getTime() + 5 * 3600 * 1000);
String time = new SimpleDateFormat("HH:mm").format(date);
time will be in your local timezone, so if you are in Pakistan it will be OK
You can use SimpleDateFormat as below:
String strDate = null;
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"hh:mm");
strDate = dateFormatter.format(yourDate);
Hope it helps.
java dateFormat is not threadsafe, use joda time lib instead:
Joda time lib download
you can use withZone method to change time with specific Timezone
DateTime userTime1 = new DateTime();
DateTime eventRecordTime = new DateTime();
userTime1 = DateTime.parse("2012-07-05T21:45:00+02:00");
eventRecordTime = DateTime.parse((String) jo.get("start_time"));
DateTimeZone dtz = userTime1.getZone();
System.out.println(eventRecordTime.withZone(dtz).toString());
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");
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.