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"
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));
if i try the below code for eg:
i will set the date as Fri July 25 2014 10:00 AM. which gives date in milliseconds as 1402080056000,
now if i try to read the same milliseconds to date as below
long time = 1402080056000;
Date mydate = new Date(time);
mydate variable shows date as Sat Jun 25 00:10:56 IST 2014
String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));
with the above statement in DateTimeString i get date as Jun 25 , 2014 12:10:56 AM
How to read the datetime present in 1402080056000 to Fri July 25 2014 10:00 AM
Just need to work on the format string,
String dateTimeString=
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));
Explicitly set time zone:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));
Also, this would be useful Regarding Timezones and Java
try below code:
private String convertMilliToDate(long timestamp) {
DateFormat formatter = new SimpleDateFormat("YOUR DATE FORMAT");
// Create a calendar object that will convert the date and time value in
// milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return formatter.format(calendar.getTime());
}
Try this:
String date= DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(date_in_milis)).toString();
Date-time work is easier using the Joda-Time library, which works on Android.
long millis = 1402080056000L;
DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( millis, timeZoneIndia );
DateTime dateTimeFrance = dateTimeIndia.withZone( DateTimeZone.forID( "Europe/Paris" ) );
DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );
To parse a string as a date-time, search StackOverflow for "Joda parse" to find many examples.
In the following code myLeg.TimeStamp is a String that has "Feb 26 2014 12:31:23 PM"
myRTleg.Tstamp is a Date.
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy HH:mm:ss a");
myRTleg.TStamp = formatter.parse(myLeg.TimeStamp);
String debugStr = myRTleg.TStamp.toString();
DebugStr has the same exact date in it but it has AM instead of PM !!
Why is it doing this?
Thanks,
Dean
From
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
H Hour in day (0-23)
...
h Hour in am/pm (1-12)
so try
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy hh:mm:ss a");
The AM/PM is probably defaulting to AM, and being ignored because the hours are HH (military) instead of hh
You didn't provide the full input and output dates. Check the timezone of your input date and output date to see if they are different. It could just be the same date in time but formatted in using different time zones.
You need to use hh for hours instead of HH. HH is used for Military time.
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
I have seconds from epoch time and want to convert it to Day-Month-Year HH:MM
I have tried following but it gives me wrong value.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
Above code is not working properly am i doing anything wrong here.
For example if seconds = 1299671538
then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
For example if seconds = 1299671538 then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
You have integer overflow. Just use the following (notice "L" after 1000 constant):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000L);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
or better use SimpleDateFormat class:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm");
String dateString = formatter.format(new Date(seconds * 1000L));
this will give you the following date string for your original seconds input:
Wednesday, March 9, 2011 13:52
You need to use
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
instead of
Calendar cal = Calendar.getInstance();
because UTC time from seconds depends on Timezone.
You don't need a calendar in this case, you can simply use the constructor new Date(1000 * seconds)
Then use a SimpleDateFormat to create a String to display it.
For a full explanation on using SimpleDateFormat go here.
The answer to this question though is that you need to use long values instead of ints.
new Date(1299674566000l)
If you don't believe me, run this:
int secondsInt = 1299674566;
System.out.println(new Date(secondsInt *1000));
long secondsLong = 1299674566;
System.out.println(new Date(secondsLong *1000));
I can confirm that answer from #Idolon is working fine, simple snippet is below...
long created = 1300563523;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
String dateString = formatter.format(new Date(created * 1000L));
using SimpleDateFormat is better way, change the format as you need.
Won't it work wihtout Calendar, like below? Haven't run this piece of code, but guess it should work .
CharSequence theDate = DateFormat.format("Day-Month-Year HH:MM", objDate);