I want to set a notification for this I want to get the date and time. In my database I have saved alert time and alert date differently.
alert date format is:
SimpleDateFormat df = new SimpleDateFormat("d MMM yyyy");
alert time format is:
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
Now I tried to concatenate the alert time string and alert date string and format it into date object but it is not getting format.
tried to convert into date:
public void setAlertTime() {
try {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
mAlertDateTime = mAlertDate + mAlertTime;
date = simpleDateFormat.parse(mAlertDateTime);
Log.d("AlertDate",String.valueOf(date));
Toast.makeText(AddTaskActivity.this,String.valueOf(date),Toast.LENGTH_SHORT).show();
} catch (ParseException ex) {}
}
I am not getting date value. Tried to print date value in log also in toast but it doe not show anything. What's going wrong?
You need to change two things:
add an space when concatenating date
change your SimpleDateFormat (concatenating the DateFormats with a space also):
String mAlertDate = "10 Mar 2016";
String mAlertTime = "8:00 PM";
String mAlertDateTime = mAlertDate + " " + mAlertTime;
// ↑ space!!!
// date format concatenating other date formats
SimpleDateFormat dft = new SimpleDateFormat("d MMM yyyy HH:mm a");
Date d = dft.parse(mAlertDateTime);
System.out.println(dft.format(d));
OUTPUT:
10 mar 2016 08:00 AM
for concatenate day if you merge the format then your format is
SimpleDateFormat timeStampFormat = new SimpleDateFormat("d MMM yyyy HH:mm a");
Now Add the date and time with space so it is form of format like this
mAlertDateTime = mAlertDate + " " + mAlertTime;
now Parse it to format.
date = timeStampFormat.parse(mAlertDateTime);
define your convert format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
and finally parse it.
date2 = simpleDateFormat.parse(simpleDateFormat.format(date));
Note : the Upper thing I am giving Explanation.
Now Do Just this :
Define this global :
String mAlertDate = "4 May 2016";
String mAlertTime = "01:30 PM";
Date date,date2;
and in try catch use write this code ,
try {
SimpleDateFormat timeStampFormat = new SimpleDateFormat("d MMM yyyy HH:mm a");
mAlertDateTime = mAlertDate + " " + mAlertTime;
date = timeStampFormat.parse(mAlertDateTime);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
date2 = simpleDateFormat.parse(simpleDateFormat.format(date));
Toast.makeText(AddTaskActivity.this, date2.toString() ,Toast.LENGTH_SHORT).show();
} catch (ParseException ex) {}
}
Change SimpleDateFormat parameters to fit the input.
Add a space between 'mAlertDate' and 'mAlertTime'.
Don't forget to set the locale.
String mAlertDate = "10 Mar 2016";
String mAlertTime = "08:00";
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.ENGLISH);
String mAlertDateTime = mAlertDate + " " + mAlertTime;
try {
date = simpleDateFormat.parse(mAlertDateTime);
} catch (ParseException ex) {
Logger.getLogger(LectorPDF.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(date);
Try adding space between date and time
public void setAlertTime() {
try {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
mAlertDateTime = mAlertDate + " " + mAlertTime;
date = simpleDateFormat.parse(mAlertDateTime);
Log.d("AlertDate",String.valueOf(date));
Toast.makeText(AddTaskActivity.this,String.valueOf(date),Toast.LENGTH_SHORT).show();
} catch (ParseException ex) {}
}
Related
I am trying to convert the following data time to EEE, d MMM yyyy format but kept getting Unparsable date format. Can some one please help. I couldn't find anything on the web that helped.
Here is the code
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date convertedDateStart = new Date();
try {
convertedDateStart = dateFormat.parse(datestr);
camp_new.startdate = convertedDateStart;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help wound be greatly appreciated. Thanks in advance
The date string "2017-01-12T00:00:00Z" cannot be parsed with your format "EEE, d MMM yyyy HH:mm:ss Z"; the fields are the wrong type or in the wrong place.
The format for your datestr "2017-01-12T00:00:00Z" should be:
yyyy-MM-dd'T'HH:mm:ssZ
You need to initialize the SimpleDateFormat with this format to parse datestr.
The format "EEE, d MMM yyyy HH:mm:ss Z" would parse a date that looks like
Thu, 1 Jan 2017 00:00:00 Z
Read the docs to understand the difference between parse() and format().
You have to parse it first, store it as a Date object and then use another SimpleDateFormat to format in the desired way.
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = dateFormat.parse(datestr);
String convertedDateStart = dateFormat2.format(date);
Well This will Work For you if you want to FORMAT and get a Date String with that format..
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
try {
date = dateFormat.parse(datestr);
} catch (ParseException e) {
e.printStackTrace();
}
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
datestr = dateFormat.format(date);
System.out.println(datestr);
This will give you Thu, 12 Jan 2017 00:00:00 +0430 Date String.
I'm getting date from JSON Response in string format , but right now i want to that staring date into date format. How can i convert this strActiondate﹕ = 9/28/2015 6:52:41 PM into date , so that i can insert into Sq-lite database.
I have trying this way
strActiondate = jobjVessels.getString("actionDate");
Log.e("strActiondate ", " = " + strActiondate);
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(strActiondate)));
Log.e("netDate "," = "+netDate);
}
catch(Exception ex){
ex.printStackTrace();
}
I want date in same format = 9/28/2015 6:52:41 PM
This DateFormat: MM/dd/yyyy, is not valid for this date:9/28/2015 6:52:41 PM.
You need a good SimpleDateFormat that matches this string:
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
m Minute in hour Number 30
s Second in minute Number 55
So this:
String strActiondate = "9/28/2015 6:52:41 PM";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
System.out.println(netDate);
Will output:
Mon Sep 28 06:52:41 CEST 2015
In your code:
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
Log.d("netDate "," = "+netDate); // log.e is an error... so better d or i
}
Use this code
strActiondate = jobjVessels.getString("actionDate");
Log.e("strActiondate ", " = " + strActiondate);
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
Log.e("netDate "," = "+netDate);
}
catch(Exception ex){
ex.printStackTrace();
}
I want to change my date format that is in as
String date ="29/07/13";
But it is showing me the error of *Unparseable date: "29/07/2013" (at offset 2)*
I want to get date in this format 29 Jul 2013.
Here is my code that i am using to change the format.
tripDate = (TextView) findViewById(R.id.tripDate);
SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy");
try {
oneWayTripDate = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
tripDate.setText(oneWayTripDate.toString());
Try like this:
String date ="29/07/13";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat output = new SimpleDateFormat("dd MMM yyyy");
try {
oneWayTripDate = input.parse(date); // parse input
tripDate.setText(output.format(oneWayTripDate)); // format output
} catch (ParseException e) {
e.printStackTrace();
}
It's a 2-step process: you first need to parse the existing String into a Date object. Then you need to format the Date object into a new String.
Change the format string to MM/dd/yyyy, while parse() and use dd MMM yyyy while format().
Sample :
String str ="29/07/2013";
// parse the String "29/07/2013" to a java.util.Date object
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(str);
// format the java.util.Date object to the desired format
String formattedDate = new SimpleDateFormat("dd MMM yyyy").format(date);
DateFormat df = new SimpleDateFormat("dd / MM / yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());
Calendar ci = Calendar.getInstance();
CiDateTime = "" + (ci.get(Calendar.MONTH) + 1) +
"/" + ci.get(Calendar.DAY_OF_MONTH) +
"/" + ci.get(Calendar.YEAR);
String visitdatetime = CiDateTime + "" +
ci.get(Calendar.HOUR_OF_DAY) + ":" +
ci.get(Calendar.MINUTE) + ":" +
ci.get(Calendar.SECOND);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
I am getting from the server in 24 hours format.but want to show my date like MM/dd/yyyy hh:mm AM/PM like 12 hrs format.
Did you try this ?
SimpleDateFormat dateFromat= new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
Date today = new Date();
String todayStr = dateFromat.format(today);
Below function may be useful to you. This will convert time from 24 hours to 12 hours
public static String Convert24to12(String time)
{
String convertedTime ="";
try {
SimpleDateFormat displayFormat = new SimpleDateFormat("hh:mm a");
SimpleDateFormat parseFormat = new SimpleDateFormat("HH:mm:ss");
Date date = parseFormat.parse(time);
convertedTime=displayFormat.format(date);
System.out.println("convertedTime : "+convertedTime);
} catch (final ParseException e) {
e.printStackTrace();
}
return convertedTime;
//Output will be 10:23 PM
}
public static final String TIME_FORMAT = "hh:mm aa";
SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT);
Calendar ATime = Calendar.getInstance();
String Timein12hourFormat = TimeFormat.format(ATime.getTime());
try with this answer this is shortest and best answer on stack.
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
Datetime = sdf.format(c.getTime());
System.out.println("============="+Datetime);
Result:-=========2015-11-20 05:52:25 PM
Here is the answer to get a data in US-English Format date/time using a 12 hour clock.
DateTime.Now.ToString("MM/d/yyyy hh:mm:ss tt");
01/16/2014 03:53:57 PM
hh:mm a
Adding the 'a' should do the trick in displaying the am/pm
In angular i am using angular-bootsrap-datetimepicker and to display the selected date and time i am using this syntax
data-ng-model="event.start | date:'dd-MM-yyyy hh:mm a'"
use below format to 12 hour with am/pm
MM/dd/yyyy HH:mm:ss a
I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me
Thank you
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "2011/11/12 16:05:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use the SimpleDateFormat for the same kinds of any date operations.
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");
// 'a' for AM/PM
Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());
// Now formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
just use the Time class. Try something similar to this.
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
If you really need a Date object just try this.
Date date = new Date(Long.parse(yourTimeString));
Use android.text.format.Time to the conversion. You can pass the time as text and it will return you the time in desired format by using timeInstance.format("");
you need to provide formatter.
Refer to following:
Time : http://developer.android.com/reference/android/text/format/Time.html
Formatter: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
you can use any combination of formatter string to work with it :)
you can use SimpleDateFormat or DateFormat for this here is the example
SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
Date d = gsdf.parse("2011/12/13 16:17:00");
System.out.println("new date format " + sdf.format(d));
}catch(Exception){
// handle exception if parse time or another by cause
}
public static String getFormatedDate(String strDate, String sourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and use like
getFormatedDate(strDate,
"yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy")