I want to calculate 4 months after the date I pulled from the database. How can I do that. The output of history is as follows.
Wed Nov 27 14:42:23 GMT+03:00 2019
JSONObject form_tarih2 = jObj.getJSONObject("form_tarih2");
String date = form_tarih2.getString("date");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date calculateDate = sdf.parse(date);
Try this:
JSONObject form_tarih2 = jObj.getJSONObject("form_tarih2");
String date = form_tarih2.getString("date");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date calculateDate = sdf.parse(date);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(calculateDate);
calendar.add(Calendar.MONTH,4);
You can add/sub days, months, year etc according to your need using Calendar
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 want to display date in month and date format(ex:May 26).But when I am trying to use it was showing cannot resolve method.
String dateStr = DateFormat.format("MMM dd", cal.getTime().getTime()).toString();
You can use SimpleDateFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd", Locale.US);
Date date = new Date(cal.getTimeInMillis());
String dateStr = dateFormat.format(date);
Try
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
Date date = sdf.parse(cal.getTime().getTime());
String dateStr = sdf.parse(date);
try this one
SimpleDateFormat format = new SimpleDateFormat("MMM dd");
String date = format.format(Date.parse(DateFormat.getDateTimeInstance().format(new Date())));
Here is some example code:
DateFormat.getDateInstance().format(new Date(0));
DateFormat.getDateTimeInstance().format(new Date(0));
DateFormat.getTimeInstance().format(new Date(0));
The above three lines will give:
Dec 31, 1969
Dec 31, 1969 4:00:00 PM
4:00:00 PM 12:00:00 AM
You can use SimpleDateFormat.
String dat = "2017-01-29 09:49:22"
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd");
Date date = simpleDateFormat.parse(dat);
String dateString = simpleDateFormat.format(date);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date convertedDate = format.parse(selectedDate);
Calendar cal = Calendar.getInstance();
cal.setTime(convertedDate);
dateSelection.setText(String.format(Locale.US, "%1$td %1$tB, %1$tY", cal));
output: 9 May, 2017
I want convert timestamp to date and set time in this day to 12.00.
How can I do this?
If I use this:
Date date = new Date(event.getActionDate()*1000);
I can't set hour or minutes to this date, becouse methods for this operations are deprecated.
Explanation:
I have timestamp -
1461924872 // Fri, 29 Apr 2016 10:14:32 GMT
I want change hour in this timestamp (10:14 to 00.00).
long timeInMillis = 1461877200000l;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
System.out.println("Date1:"+calendar.getTime().toString());
calendar.set(Calendar.HOUR_OF_DAY, 0); //For 12 AM use 0 and for 12 PM use 12.
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime();
System.out.println("Date2:"+date.toString());
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(time) * 1000));
Date date = new Date();
date = sdf.parse(localTime);
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
}
catch(Exception ex){
return dateInStr;
}
}
let me know if it is working?
Need to show time in GMT...but it showing in UTC
long m=Long.parseLong("1444588200000");
Date localTime = new Date(m);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
Locale.getDefault());
DateFormat date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
String time = date.format(localTime);
infoip.setText(" "+time);
Try this,
final Date currentTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
// Give it to me in GMT time.
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("GMT time: " + sdf.format(currentTime));
Try like this hope work.
Calendar cal = Calendar.getInstance();
// setting the timezone for which you want
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis();
// The date is in your home timezone
Date date = cal.getTime();
TimeZone tz = TimeZone.getTimeZone("GMT");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS", Locale.GMT);
sdf.setTimeZone(tz);
// Then we do the conversion to convert the date you provided in milliseconds to the GMT timezone
String result = sdf.parse(date);
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;
}