This question already has answers here:
android java parse date from string
(3 answers)
Closed 5 years ago.
I have a date string in this format '20161201'.
How to make this a datetime string to be july 2016?
I want to show 6 months before that datetime string.
Is it possible?
Also how do I convert just '20161201' to Dec 2016?
you change the format using the following code,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
SimpleDateFormat expectedSDF = new SimpleDateFormat("MMMM yyyy", Locale.getDefault());
Date date = simpleDateFormat.parse(dateString);
String newFormatString = expectedSDF.format(date);
And for the countdown, use CountDownTimer
https://developer.android.com/reference/android/os/CountDownTimer.html
For convert '20161201' to 'Dec 2016':
//Convert string to date
String dateString = "20161201";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = new Date();
try {
convertedDate = parseDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
//Convert date to string
SimpleDateFormat outputDateFormat = new SimpleDateFormat("MMM yyyy");
String result = outputDateFormat.format(convertedDate);
//Print the result
System.out.println(result);
In '6 months' to millisecond is 15778476000. You can get the date after 6 months in millisecond by using
long resultdate = convertedDate.getTime() - 15778476000L;
and then
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(resultdate);
String result = outputDateFormat.format(calendar.getTime());
to get the result
https://developer.android.com/reference/java/text/SimpleDateFormat.html
https://developer.android.com/reference/java/util/Date.html
https://developer.android.com/reference/java/util/Calendar.html#setTimeInMillis(long)
how to make this datetime string to be july 2016
I want to show 6 months before that datetime string
July is 5 months...
Here's the 6 month answer
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyyMMdd").parse("20161201"));
c.set(Calendar.MONTH, c.get(Calendar.MONTH)-6);
String output = new SimpleDateFormat("MMMM yyyy").format(c.getTime()));
// June 2016
Related
This question already has answers here:
Change date string format in android
(8 answers)
Date format conversion Android
(9 answers)
Closed 2 years ago.
I get datetime data from soap web service to get string:2020-05-03T00:00:00.
Is there a way to split the string into dd / mm / yyyy, and if it is null then omitted?
I am using a substring(0,10) , but it doesn't work very well.
You can do it this way.
Parse the input string.
LocalDateTime dateTime = LocalDateTime.parse("2020-05-03T00:00:00");
Generate text representing the value of that LocalDateTime object.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateTimeString = dateTime.format(formatter);
System.out.println(dateTimeString);
03/05/2020
For early Android before 26, see the ThreeTenABP & ThreeTen-Backport projects, a back-port of most of the java.time functionality.
try with this
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
date = inputFormat.parse("2020-05-03T00:00:00");
String formattedDate = outputFormat.format(date);
System.out.println("coverted: "+formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: 03-05-2020
You can use java Calendar class to get Date, Month and Year as shown in below
String s = "2020-05-03T00:00:00";
Calendar calendar = new GregorianCalendar();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj = null;
try {
dateObj = format.parse(s);
calendar.setTime(dateObj);
int date = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH) + 1;// As start from 0
int year = calendar.get(Calendar.YEAR);
System.out.println("Formatted Date -> "+ date + "/" + month + "/" +year);
} catch (ParseException e) {
e.printStackTrace();
}
You can do many more thing after converting it to Calendar class object, like hour, minutes, day of week etc
This question already has answers here:
How to format date and time in Android?
(26 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have Date like this Sun May 20 09:18:44 GMT+04:30 2018 how i can get clock from this Date,
i want result like this 09:18 am
Log.v(TAG,"date "+ date.toString());// output is Sun May 20 09:18:44 GMT+04:30 2018
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);//NullPointerException
your code is working you just have to initialize Date class's object
Date date = new Date();
Date date = new Date();
Log.v(TAG,"date "+ date.toString());
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);
Log.v(TAG,"date "+ time);
Try this code..
SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
SimpleDateFormat sdf4 = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
Date d1 = null;
try{
d1 = sdf3.parse("Sun May 20 09:18:44 GMT+04:30 2018");
String date=sdf4.format(d1);
System.out.println("time..." + date);
}catch (Exception e){ e.printStackTrace(); }
System.out.println("check..." + d1);
This question already has answers here:
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Closed 5 years ago.
I am trying to convert string of format yyyy-mm-dd to dd-MMM-yy. I am getting correct year and days but for month it is showing only jan irrespective of my input. How to fix it?
String input = "2013-09-14";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
Date date = null;
try {
date = format1.parse(input);
String temp = format2.format(date);
System.out.println(temp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
14-Jan-13
But I should get:
14-Oct-13
mm is for minute
you need MM or MMM for month.
See SimpleDateFormat for reference.
mm is for minutes. MM is for months.
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
I would suggest latest API LocalDate which not requires try/catch and which is easier to use :
String input = "2013-09-14";
LocalDate inputDate = LocalDate.parse(input, DateTimeFormatter.ISO_DATE);
String format = inputDate.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
System.out.println(format);
DateTimeFormatter.ISO_DATE is shortcut for DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate inputDate = LocalDate.parse(input); would also work because ISO_DATE the default format
DateTimeFormatter doc (patterns)
The pattern is case sensitive:
MM is month, mm is Minute:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
For more Information read The javadoc of SimpleDateFormat
personally i used a pattern to do this,
LocalTime w = LocalTime.MIDNIGHT.plus(d);
s = DateTimeFormatter.ofPattern("HH:mm:ss").format(w);
and i advise you to use java 8 date type instead
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
string : 2014-04-25 17:03:13
using SimpleDateFormat is enough to format?
or
otherwise i will shift to any new API?
Date date = new Date(string);
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
out.println( dateFormat.format (date));
My expected result is (India zone):
Date : 25-04-2014
Time : 05:03 PM
Remembering that Date objects have no inherent format, you need two DateFormat objects to produce the result you seek - one to parse and another to format:
String input = "2014-04-25 17:03:13";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("'Date : 'dd-MM-yyyy\n'Time : 'KK:mm a");
System.out.println(outputFormat.format(inputFormat.parse(input)));
Output:
Date : 25-04-2014
Time : 05:03 PM
Note the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.
I custom onTimeSet() function . Send the hour and minutes to it. It will return the time with format am and pm
public static String onTimeSet( int hour, int minute) {
Calendar mCalen = Calendar.getInstance();;
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format_local = mCalen.get(Calendar.HOUR);
int hourOfDay_local = mCalen.get(Calendar.HOUR_OF_DAY);
int minute_local = mCalen.get(Calendar.MINUTE);
int ampm = mCalen.get(Calendar.AM_PM);
String minute1;
if(minute_local<10){
minute1="0"+minute_local;
}
else
minute1=""+minute_local;
String ampmStr = (ampm == 0) ? "AM" : "PM";
// Set the Time String in Button
if(hour12format_local==0)
hour12format_local=12;
String selecteTime=hour12format_local+":"+ minute1+" "+ampmStr;
retrun selecteTime;
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
more patterns you can find here
Try given below sample code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
//Output: 2013-05-20 10:16:44
For more functionalities on Data and Time try Joda-Time API .
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi, I have a string like:
String selectedDate = "Fri Jan 17 00:00:00 CAT 2014";
I need to format it to:
DD.MM.YYYY = 17.01.2014 00:00:00
Can anyone help please see code below:
String date = getDate(selectedDate);
private String getDate(String inpuDate) {
Date date = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss ").parse(inputDate);
return new SimpleDateFormat("dd.mm.yyyy hh:mm:ss").format(date);
}
Try the following:
String date = getDate(selectedDate);
private String getDate(String inpuDate) {
Date date = new SimpleDateFormat("EEE MMM d hh:mm:ss z yyyy").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy hh:mm:ss").format(date);
}