when i am print System.currentTimeMillis()
give me :
11-03 14:47:05.400: INFO/System.out(7579): date is :: 14475410111
What is the correct procedure to get entire date with time.?
To get current date and time in Android, You can use:
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
Output:
Current time => Thu Nov 03 15:00:45 GMT+05:30 2011
FYI, once you have this time in 'c' object, you can use SimpleDateFormat class to get date/time in desired format.
Final Solution:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sring formattedDate = df.format(c.getTime()); // c is Calendar object
System.out.println("========> formatted date => "+formattedDate);
Output:
========> formatted date => 2011-11-03 15:13:37
Date now = new Date(System.currentTimeMillis());
By the way : currentTimeMillis()
Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.
Yes This is the correct way to get the current time and date. afte that you need to convert it to desired format.
public static String getDateFromTimeStamp(String timeStamp){
return new Date(Long.parseLong(timeStamp)).toString();
}
Use the Date class.
(What makes you think that's wrong? You're asking for the time in milliseconds.)
Use SimpleDateFormat
Related
Explanation:
I have time in GMT format i need to convert into IST. I did already but for 24 hour i want to get in 12 hour format with AM/PM.
here is my code
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date1;
date1=utcFormat.parse(time);
Log.e("IST",""+date1.toString());//After convert in IST i got Sat Mar 26 19:30:00 GMT+05:30 2016
Date timestamp;
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
timestamp = utcFormat.parse(time);
Log.e("timestamp", "" + timestamp.toString()); //After convert in UTC i got Sat Mar 26 19:30:00 GMT+05:30 2016
Log.e("Time",""+time.toString());//This is my time 2016-03-26T14:00+00:00
Problem is i got the same time for IST and GMT.
What will i need to do so than i will get a time in 12 hours???
Please help me to solve out this problem.
Remember that the Date class does not hold any actual timezone information, it is just a long millisecond value from UNIX epoch time. And the TimeZone of the SimpleDateFormat is only relevant when you display the date, not when you parse it. So in order to print your Date as an IST date, you must use that format when printing the Date:
// Parse your time string
DateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
Date date = myFormat.parse(time);
// Set a new format for displaying your time
DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
// This will print the IST time in the format specified: 2016-03-25T09:42:07+5:30
String istTime = isoFormat.format(date);
Log.d(istTime);
As for the 12 hour format, you can set that with a new SimpleDateFormat when you print the time.
/**
* 'hh' is the 12-hour time format, while 'HH' is the 24-hour format
* 'hh' will always print two digits, while 'h' will drop leading zeros
* 'a' is the AM/PM marker
*/
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
// This will print the current time, for example: 09:42 AM
Log.d(dateFormat.format(new Date()));
Android also has a helpful DateUtils class, that will format the Date based on the settings of the current device.
Here i was created a another object of DateFormat and pass a parameter inside it' constructor.
//hh:mm a format of 12 hour with leading 0 before the hours, mm is for minutes and (a) is for AM/PM format.
//where as HH is provides the format of 24 hours.
Here i got a 12 hours format.
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("hh:mm a");
String ist=outputFormat.format(utcFormat.parse(time));
In my app i need to convert my current date into UTC format, i can successfully converted, now problem is i need 24 hours format check it out my below code
public static String CurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
LogUtil.d("new Date()" + new Date());
return df.format(new Date());
}
now my CurrentDate returns 1.45 but i need 13.45 how can i convert utc in 24 hours format?
i have search through google but dint get proper answer, all suggestions are most welcome
Thanks in advance
Changing the hour part at your SimpleDateFormat constructor call from hh to HH does the job:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Output:
2015-07-13 13:53:02
See also Table of Date and Time Patterns
As Jon Skeet said, check the String you pass on SimpleDateFormat() : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You need to replace "hh" with "HH" so it will become "yyyy-MM-dd HH:mm:ss"
Of course if you only need the hour and minute that should be "HH:mm"
I would like to convert a Date from my current TimeZone to UTC.
The results are not understandable for me.
Code:
public static String convertToUTC(String dateStr) throws ParseException
{
Log.i("myDateFunctions", "the input param is:"+dateStr);
String uTCDateStr;
Date pickedDate = stringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
Log.i("myDateFunctions", "the input param after it is converted to Date:"+pickedDate);
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
Log.i("myDateFunctions:", "my current Timezone:"+tz.getDisplayName()+" +"+(tz.getOffset(now.getTime()) / 3600000));
// Convert to UTC
SimpleDateFormat converter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setTimeZone(TimeZone.getTimeZone("UTC"));
uTCDateStr = converter.format(pickedDate);
Log.i("myDateFunctions", "the output, after i converted to UTC timezone:"+uTCDateStr);
return uTCDateStr;
}
And LogCat results are:
03-29 20:31:46.804: I/myDateFunctions(18413): the input param is:2014-04-29 20:00:00
03-29 20:31:47.005: I/myDateFunctions(18413): the input param after it is converted to Date:Tue Apr 29 20:00:00 CEST 2014
03-29 20:31:47.005: I/myDateFunctions:(18413): my current Timezone:Central European Time +1
03-29 20:31:47.005: I/myDateFunctions(18413): the output, after i converted to UTC timezone:2014-04-29 18:00:00
As you can see:
My TimeZone is CET (GMT+1)
Then why if my input is 20:00 i get back 18:00 instead of 19:00 ?
The problem is daylight savings time. UTC doesn't have it, if yours does it will increase the difference by 1 hour during part of the year.
The answer by Game Sechan appears to be correct.
I just want to show how much easier this work is when using Joda-Time or java.time rather than the notoriously troublesome java.util.Date and .Calendar classes.
Joda-Time
In Joda-Time 2.4.
String inputRaw = "2014-04-29 20:00:00";
String input = inputRaw.replace( " ", "T" );
DateTimeZone timeZoneIntendedByString = DateTimeZone.forID( "America/Montreal" ); // Or DateTimeZone.getDefault();
DateTime dateTime = new DateTime( input, timeZoneIntendedByString );
DateTime dateTimeUtc = dateTime.withZone( DateTimeZone.UTC ); // Adjust time zones, but still same moment in history of the Universe.
i got this time: 1365443145000
i need to get Sensible date (dd/MM/yyyy)
i try : Date d = new Date(1365443145000L * 1000);
but i got: 45239-03-04
Instead of Correct result is: 08/04/2013 20:45
Dont' multiply with 1000 as you are getting the time in Milliseconds. You can convert date in milliseconds to Date by using the below code, one by using Date constructor and second by using Calendar
call it like this
Date d=new Date(1365443145000L);
System.out.println(d.toString());
output:
Mon Apr 08 17:45:45 UTC 2013
Check this link
http://www.browxy.com/SubmittedCode/17928
Edit:
To convert required format use this code
Calendar caldate=Calendar.getInstance();
caldate.setTimeInMillis(1365443145000L);
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy hh:mm");
String strdate=sdf.format(caldate.getTime());
System.out.println(strdate);
Hi does anyone know how to get the month as a two digit number for example 04 or 05?
Try this
dateFormat = "MM";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String month = sdf.format(date);
For more details please check Formatting Month in Android
Also check SimpleDateFormat.
HI :) what type of language that you use? javascript ou java?
in java you can try :
Date x=new Date();
System.out.println(x); // Fri Jun 05 22:43:25 BRT 2009
in javascript you can try:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
a simple way is....
String date="1";
if(Integer.parseInt(date)<10)
{
date="0"+date;
}
You can use the SimpleDateFormat to change how you want to display a date, code can be found here
and there is more about DateFormat Class here