I am trying to Convert date and time i am receiving in MM/dd/yyyy hh:mm a format to milliseconds so that i can push the date as end date in google calendar. I am trying below code but i am getting error.
Code:
String myDate = "10/20/2017 8:10 AM";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date date = sdf.parse(myDate);
long millis = date.getTime();
Error:
java.text.ParseException: Unparseable date: "10/20/2017 8:10 AM" (at offset 16)
Try this .
String myDate = "10/20/2017 8:10 AM";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm a", Locale.ENGLISH);
Date date = sdf.parse(myDate);
long millis = date.getTime();
Add Locale.ENGLISH in your code .
Your code seems to be OK, i've just added try/catch block:
try {
String myDate = "10/20/2017 8:10 AM";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date date = sdf.parse(myDate);
long millis = date.getTime();
System.out.println(date);
System.out.println(millis);
}
catch(Exception ex) {
System.out.println(ex);
}
This code prints 1508479800000 as millis. If you want to check it backwards try this:
String x = "1508479800000";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
long milliSeconds= Long.parseLong(x);
System.out.println(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));
this will give you 10/20/2017 08:10 AM.
You can use SimpleDateFormat to do it. You just have to know 2 things.
All dates are internally represented in UTC
.getTime() returns the number of milliseconds since 1970-01-01 00:00:00 UTC.
package se.wederbrand.milliseconds;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String inputString = "00:01:30.500";
Date date = sdf.parse("1970-01-01 " + inputString);
System.out.println("in milliseconds: " + date.getTime());
}
}
by this one https://stackoverflow.com/a/8826392/8456432
Related
I have this date => 2017-10-17 10:23:30 UTC TIME
I'm parsing it like this
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
TimeZone tz = TimeZone.getDefault(); // +03:00
dateFormat.setTimeZone(tz);
Date parsed_date = dateFormat.parse("2017-10-17 10:23:30");
but this gives me this
Tue Oct 17 13:23:30 GMT+03:00 2017
I want this
2017-10-17 01:23:30
What's the problem?
Thank you guys for your help that's what i wonted i turn out that if the date in UTC you should start with parsing in the utc format that will keep the date as its the for formating it to your local timezone format it with local time zone
private String getDate(String dateString) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = null;
try {
value = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm aa");
dateFormatter.setTimeZone(TimeZone.getDefault());
String dt = dateFormatter.format(value);
return dt;
}
A Date does not have a time zone. So your parsing works since Tue Oct 17 13:23:30 GMT+03:00 2017 is the same point in time as 2017-10-17 10:23:30 UTC.
The time zone is only added when you try to print a date. For example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
String strDate = dateFormat.format(parsed_date);
System.out.println(strDate);
prints:
2017-10-17 10:23:30
Update: to show a date in another format / time-zone you can use something like this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
printFormat.setTimeZone(TimeZone.getTimeZone("GMT+3"));
String strDate = printFormat.format(parsed_date);
System.out.println(strDate);
This prints:
2017-10-17 01:23:30
I am having a date string 2012-11-21 13:11:25 which I get from local database. I have to convert this according to UTC settings and display it on a particular screen. So if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.
I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 after this I need to get the time as 18:41:25 and date as 11-21-2012
Thanks in advance
Your df and inputFmt must use the same format.
But I think you should do it like this:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
Get UTC from current time :
public String getCurrentUTC(){
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
return outputFmt.format(time);
}
Best way to get formatted string of Date in required format is
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = dateFormat.format(date);
//This is my input date
String dtStart = "2019-04-24 01:22 PM";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date date = null;
try {
date = format.parse(dtStart);
getDateInUTC(date)
} catch (ParseException e) {
e.printStackTrace();
}
//This Method use for convert Date into some UTC format
public static String getDateInUTC(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateAsString = sdf.format(date);
System.out.println("UTC" + dateAsString);
return dateAsString;
}
hello I have date like..
String date="09:00 AM"
But I need 09:00:00
So I use following.
String date="09:00 AM"
SimpleDateFormat f1 = new SimpleDateFormat("hh:mm:ss");
Date d1= f1.parse(date);
But it give me parse exception.
Please help me to find this.
Thanks in advance.
That's not the correct format for parsing that date, you need something like:
"hh:mm a"
Once you have a date object, you can then use a different format to output it. The following code segment shows how:
import java.text.SimpleDateFormat;
import java.util.Date;
String date = "09:27 PM";
SimpleDateFormat h_mm_a = new SimpleDateFormat("h:mm a");
SimpleDateFormat hh_mm_ss = new SimpleDateFormat("HH:mm:ss");
try {
Date d1 = h_mm_a.parse(date);
System.out.println (hh_mm_ss.format(d1));
} catch (Exception e) {
e.printStackTrace();
}
This outputs:
21:27:00
as you would expect.
Date c = Calendar.getInstance().getTime();
//day of the month
//EEEE---full day name
//EEE- 3 chars of the day
SimpleDateFormat outFormat = new SimpleDateFormat("EEE");
String dayof_month = outFormat.format(c);
//date
//MMM--3 chars of month name
//MM--number of the month
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c);
//time
//hh---12 hrs format
//HH---24 hrs format
// a stands for AM/PM
DateFormat date = new SimpleDateFormat("hh:mm:ss a");
String localTime = date.format(c);
login_date_text.setText(dayof_month +" , "+formattedDate);
login_time_text.setText(localTime);
Log.e("testing","date ==" +dayof_month +" , "+formattedDate);
Log.e("testing","time ==" +localTime);
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")