Android Current Date and Time [duplicate] - android

This question already has answers here:
Display the current time and date in an Android application
(23 answers)
Closed 9 years ago.
I need to format a date and time as Nov 15 2012 18:55 GMT.
Time should be in GMT and 24hr format.I have tried using the following code but it gives time in 12hr format.Is there any way?
DateFormat currentDateTimeString = DateFormat.getDateTimeInstance();
currentDateTimeString.setTimeZone(TimeZone.getTimeZone("gmt"));
gmtTime = currentDateTimeString.format(new Date());

You can use DateFormats to convert Dates to Strings in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
OR
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String localTime = date.format(currentLocalTime);
System.out.println(localTime);

Related

how to convert date which is taken as string and assign some formate in android [duplicate]

This question already has answers here:
How to convert Date to a particular format in android?
(7 answers)
Closed 3 years ago.
I have date in below format coming from api response and in pojo i take it as string and now date is taken as string and my concern is Date is deprecated in api 16.
My main question is how to convert this and assign some date format to it.
"date":1561040449000"
historyList.get(position).getTxnDate() = "1561040449000"
long dateToLong = Long.parseLong(historyList.get(position).getTxnDate());
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
String dateString = sdf.format(new Date(dateToLong));
Pass your long typed "date" timestamp to this method and it will return date in specified format:
public static String getDateFromTimeStamp(long date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(new Date(date));
}

How to parse String into Gregorian Calendar? [duplicate]

This question already has answers here:
How to convert Gregorian string to Gregorian Calendar?
(3 answers)
Closed 4 years ago.
I converted Gregorian Calendar to String by using toString() method to upload my DataBase. ( Because My Database doesn't apply Calendar type )
Here Example.
String Time =
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=81,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
How do I parse it to Calendar type?
Much appreciate if you guys could help.
Use SimpleDateFormat like this.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,23); (etc)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = simpleDateFormat.format(calendar.getTime());
Well, you can save GregorianCalendar's millis in the database which will be of a long type. Here is how you do it:
long millis = GregorianCalendar.getInstance().getTimeInMillis();
and when you want the GregorianCalendar instance in your app just get the millis from the database and add those millis to GregorianCalendar instance like this :
GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
gregorianCalendar.setTimeInMillis(millis);
Don't save the string representation of Calendar. Save the timestamp as Long.
Here an example:
Calendar cal1 = GregorianCalendar.getInstance();
long timeStamp = cal1.getTimeInMillis();
Calendar cal2 = GregorianCalendar.getInstance();
cal2.setTimeInMillis(timeStamp);

How to obtain date,day,year,hours and minutes value from a string? [duplicate]

This question already has an answer here:
How to get day, month, year and hour, minutes, second from DateTime format?
(1 answer)
Closed 8 years ago.
I have a field in mysql database of type DateTime.The value in this field is sent as a string to my android app( say in the form "2014-11-21 06:00:00") .I need to obtain year,month,day,hours and minutes value from the string and set it to a calendar instance.Please help me.
String s = "2014-11-21 06:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(s));
System.out.println(calendar.getTime());
To convert your String in a Date object you can use a SimpleDateFormat:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
Date date = simpleDateFormat.parse("2014-11-21 06:00:00");
Then you can use a Calendar with the Date object you got before
Calendar c = Calendar.geInstance();
c.setTime(date);
and use c.get(Calendar.SECOND) to get the seconds, for instance

How to get standard format date in android

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.

Convert Milliseconds String to Date String [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to convert milliseconds to date format in android?
I have a Milliseconds String and want to convert it to a Date String.
Like this:
Milliseconds: 1345032624325E12 -> Date: Wed Aug 15 2012 14:10:24
How can i do this?
Hope this helps
how to convert milliseconds to date format in android?
private String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}

Categories

Resources