Android: SimpleDateFormat changes year of my date - android

I am using SimpleDateFormat to change format of date, shown below. But it somehow changes my year from for example 2016 to 2019 or 2018. How could I make it work correctly?
String date = "2016-10-22 13:45:46.000000";
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("dd.mm.yyyy HH:MM");
String newFormat = formatter.format(testDate);

You must be careful to use the right casing (either upper or lower) for all the date/time pattern strings, as specified in the SimpleDateFormat Javadoc.
Uppercase M is used for "Month in year", and lowercase m is used for "Minute in hour".
Uppercase S is used for "Millisecond", and lowercase s is used for "second in minute".
This should work properly:
String date = "2016-10-22 13:45:46.000000";
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("dd.MM.yyyy HH:mm");
String newFormat = formatter.format(testDate);

Related

Android Use SimpleDateFormat

I get a data from server like this:
data = "2015-04-24T23:00:17+08:00"
I am in UTC+8,I want get
data = "2015-04-24 23:00:17"
I program this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Data = formatter.parse(data);
But I get that
Fri Apr 24 23:00:17 格林尼治标准时间+0800 2015
How can I correct it?
if I use
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd HH:mm:ss",Locale.ENGLISH);
I get
Fri Apr 24 23:00:17 GMT+08:00 2015
It is not I want
You are missing a Locale.
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
See the documentation.
Once you parse the date you returned from server, you have a java.util.Date object. If you want to convert to your own format you have to again format the date object.
private String getDate(String serverDate) {
SimpleDateFormat serverFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = serverFormatter.parse(serverDate);
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = myFormatter.format(date);
return formattedDate;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
For your desired output you need to change the date format to
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

How to format date in Android in dd/mm/yy 00:00AM

How to format my date in Android. I have String which gets date in below format
String format = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Output
25 Nov 2013 17:39:00
I want this to be converted in below format
dd/mm/yy 5:39PM
I am using below code to get Date. Also how to get that format into String ?
Using SimpleDateFormat class you can do it.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh:mm");
String format = sdf.format(Calendar.getInstance().getTime());
please try follwing answer
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
java.util.Date date = null;
try
{
date = form.parse("2011-03-27T09:39:01.607");
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
String newDateStr = postFormater.format(date);
I got it, This has to go like this
Date today = new Date();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
String date = DATE_FORMAT.format(today);
DATE_FORMAT = new SimpleDateFormat("dd/MM/yy hh:mm:ss aa");
date = DATE_FORMAT.format(today);

Issue in SimpleDateFormat in android

I have the code to change time format
String date = "13/08/13 05.57 PM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy hh.mm a");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
I got the output as
.....Date...13-57-2013 05:57:00 PM
instead of 13-08-2013 05:57:00 PM. Please help me to solve this issue.
Problem with mm that should be use for minutes. For months you should use MM. Read more about Date and Time Patterns in Java
Use below code
String date = "13/08/13 05.57 PM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
Use 'MM' instead of 'mm' where month is expected. Take a look at Date and Time Formats for the explanation of the different format characters.

Convert a date and time string according to UTC format

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;
}

how to convert date format in android

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")

Categories

Resources