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);
Related
I am trying to pass a string of the form 12:00 into milliseconds based on the current date but I seem unable to get a good understanding of how the Calendar and Date class work to achieve this.
Now I have this code:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Calendar startTime = Calendar.getInstance();
String alarmPref = preferences.getString(PreferenceUtility.getReminderTimes(context), "12:00");
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
Date date = format.parse(alarmPref);
startTime.setTime(date);
This unfortunaltely gives me when logged like this:
Log.d(TAG, "Time to start:" + futureTime);
Log.d(TAG, "Date: " + date);
Gives the following results:
07-25 14:45:21.057 8409-8409/com.google.developer.bugmaster D/PreferenceUtility:Time PreferenceUtility: 21:20
07-25 14:45:21.057 8409-8409/com.google.developer.bugmaster D/QuizJobScheduler: Time to start:126000
Date: Thu Jan 01 12:00:00 GMT+01:00 1970
As seen the required string is 21:20 ( as expected ) but Time to start remains at the value 126000 and hence I keep getting the date to be Date: Thu Jan 01 12:00:00 GMT+01:00 1970, which of course is a reference to the epoch date and time.
How can I get a reference date and time that refers to the time of 21:20 and for the current date the app is running. Forgive me for my ignorance as I have tried so many literature with no success most likely I am unable to understand them.
Use this code:
String time = "21:20";
String[] splittedTime = time.split(":");
Calendar calendar = Calendar.getInstance();
// Set the current date and time
calendar.setTime(new Date());
// Set the desired hour and minute
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splittedTime[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(splittedTime[1]));
// Clear seconds and milliseconds
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// the variable date will get today's date and the desired time
Date date = calendar.getTime();
I hope you understand my comments in the code
Java's Date class does not provide the means to set ONLY the time while using the current calendar day. Android's Calendar class does.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 20);
This above example requires you to parse the time without a SimpleDateFormat. Splitting the string in half (using the ':') and integer parsing the two strings would give you the values to put in.
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 am building a basic logging utility class and would like to log the date and time down to the millisecond of when the log entry is created. I'm currently using:
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d )
which gives me '05-23 09:05:47'. I would like it to give me the milliseconds of when the log entry is created also and it does not appear that the DateFormat class supports millisecond retrieval.
Like the format "MM-dd hh:mm:ss:zzz" giving '05-23 09:05:47.447'.
Is it possible to do this using the DateFormat class (or a class like DateFormat)? I recognize it is possible to create another date removing the milliseconds part of this date and then subtracting the two and printing the difference but that's just silly. (:
Try this
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd hh:mm:ss SSS");
String dateToOutput = sdf.format(d);
While I think it's silly the DateFormat class doesn't allow easy formatted output of milliseconds, I realized that obtaining the milliseconds from the timestamp is actually quite easy. Every date object is representing a timestamp in milliseconds since 00:00 January 1, 1970 so the timestamp modulo 1000 gives the milliseconds.
I did
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d );
dateToOutput += "." + d.getTime() % 1000;
which, while not ideal, works fine and gives me '05-23 09:05:47.447'.
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