I have created a app which get the time from Mobile. I want to get the Date from google or by LocationManager . so how can i get it?
well you can use new Date(); and this will return simply current date and time, also you can format date using SimpleDateFormat of java.util package
for example:
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("EEE, MMM dd, yyyy");
try {
String date= simpleDateFormat.format(new Date().getTime());
} catch (ParseException e) {
e.printStackTrace();
}
so this will give output like MON, NOV 27, 2017
Related
i have done code below to change time from UTC to another time zone but code is showing only UTC time.Also after formatting to source time format it shows system time zone .
private String setTimezone(String time){
sourceformatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
dateFormatter = new SimpleDateFormat("hh:mm a");
sourceformatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.e("reicievedformat",time);
Date value = null;
try {
value = sourceformatter.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("afterfirstformat",dateFormatter.format(value));
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
time =dateFormatter.format(value);
Log.d("Finaltime",time);
return time;
}
Output:- Log values
E/reicievedformat: 12:36 PM, Mon 08 Oct 2018
D/afterfirstformat: 06:21 PM
D/Finaltime: 12:36 PM
As you can see I'm getting 12:36 PM, Mon 08 Oct 2018 ("UTC") and I want to convert to IST, but the final time, 12:36 PM, doesn’t seem to have been converted.
IST in java stands for "Israel Standard Time".
Use this for "Indian Standard Time"
dateFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
The date & time APIs in Java gives you headache.
I use this library by Daniel Lew.
https://github.com/dlew/joda-time-android
Try this
public static String getDateOut(String ourDate) {
try
{
//be sure that passing date has same format as formatter
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = formatter.parse(ourDate);
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a"); //this format changeable
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
ourDate = dateFormatter.format(value);
}
catch (Exception e)
{
ourDate = "00-00-0000 00:00";
}
return ourDate;
}
I have a bit of a dilemma. I have a method that's supposed to convert a given string to the type Date. For some reason I can print out the Date on the screen however the parser returns null when I try to retreive the date from the method.
An example string that is used as the parameter: Thu Aug 10 07:23:00 EEST 2017
public Date convertStringToDate(String sDate) {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());
try {
Date date = format.parse(sDate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
You are declaring a second variable named date in your try body. Remove the Date portion (which makes it local). Change
Date date = format.parse(sDate);
to
date = format.parse(sDate);
(Posted on behalf of the OP.)
I had declared the date variable somewhere outside the method and to solve this problem I had to declare a local one. Down below is the updated working code:
public Date convertStringToDate(String sDate) {
Date date;
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());
try {
date = format.parse(sDate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Because of the consistency of the problem, and the fact that I am in GMT-5, I believe this to be a timezone issue. My code is below
MediaMetadataRetriever mdr = new MediaMetadataRetriever();
mdr.setDataSource(path);
date = mdr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
try {
date = new SimpleDateFormat("MMM dd, yyyy h:mm a", Locale.getDefault()).format(new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.getDefault()).parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
Several things I did to fix it include the following code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.getDefault());
sdf.setTimezone(TimeZone.getDefaultTimezone);
try {
date = new SimpleDateFormat("MMM dd, yyyy h:mm a", Locale.getDefault()).format(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
However, nothing changed. I would like to be able to use the default timezone for each device.
Any help is appreciated
EDIT: So the timezone works if I put
sdf.setTimeZone(TimeZone.getTimeZone("Eastern Standard Time"));
but not with
sdf.setTimeZone(TimeZone.getDefault());
Also, here is some sample input/output with the TimeZone.getDefault() method:
input: 20170121T212723 output: Jan 22, 2017 2:27 am (note how the raw date/time is January 21st, but the formatted one is January 22nd) Expected output would be Jan 21, 2017 9:27 pm
So maybe this question is more about why doesn't TimeZone.getDefault() return the right timezone?
Maybe it will be useful for someone.
The reason is that TimeZone.getDefault () returns a response in the form of a ZoneInfo object while getTimeZone ("Eastern Standard Time") returns a response in the form of a SimpleTimeZone object.
So this solution worked for me in kotlin (I think the result will be the same in java):
sdf.timeZone(TimeZone.getTimeZone(TimeZone.getDefault().toString()))
I'm trying to convert a String that represents a date stored in SQLITE.
The date was stored into sqlite as follows:
Date date;
date.toString();
According with Java documentation, toString() method:
Returns a string representation of this Date. The formatting is
equivalent to using a SimpleDateFormat with the format string "EEE MMM
dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00
PDT 1999". The current default time zone and locale are used. If you
need control over the time zone or locale, use SimpleDateFormat
instead.
Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.
The String comes from sqlite:
Mon Jan 20 18:26:25 BRT 2014
So, I do:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");
What I'm doing wrong?
Thanks.
try this code
String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Try this:
String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = pre.parse(w);
System.out.println(sdf.format(date));
}catch(Exception e){
e.printStackTrace();
}
Output:
20/01/2014
Formatter for storing and restoring data value in format dd/MM/yyyy
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Storing data
String dataAsString = simpleDateFormat.format(date); // 20/01/2014
Restoring data
Date data = simpleDateFormat.parse(dataAsString);
I'm having problem parsing the tag from an RSS Feed.
The format is like this: Mon, 16 Apr 2012 16:42:30 +0000
I created a function parseDate which does the trick, but the fact is it parses the date using Locale.US, which returns the date but using the US locale, so it returns the time +2 hours. If I don't provide the Locale.US parameter, I get a ParseException.
How can I accomplish a correct parsing so the date provided is correct for any Local?
Here's the function:
public String parseDate (String dateraw){
String returndate;
try {String format = "EEE, dd MMM yyyy kk:mm:ss Z";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss z",Locale.US);
Date formatedDate = sdf.parse(dateraw);
Calendar c= Calendar.getInstance();
c.setTime(formatedDate);
returndate=""+c.get(Calendar.DAY_OF_MONTH)+"/"+c.get(Calendar.MONTH)+"/"+c.get(Calendar.YEAR)+" "+c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE);
return returndate;
} catch (ParseException e) {
e.printStackTrace();
// TODO Auto-generated catch block
return "NO DATE AVAILABLE";
}
}
Your code works fine. The date is parsed and is not timezone dependent.
Try to print your current timezone or calendar timezone and see if that is correct:
//...
Calendar c = Calendar.getInstance();
c.setTime(formatedDate);
Log.i(TAG, c.getTimeZone().getID());
Log.i(TAG, TimeZone.getDefault().getID());
//...