This question already has answers here:
Convert string to Date in java
(6 answers)
Closed 8 years ago.
I have string date time "2014-10-13 18:22:54.71", I want to convert that to Date and
I'll use that algorithm
public static boolean IsthatMorethanTwo(String DateString) {
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(new Date(DateString));
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
return days >= 2;
}
How can I do this ?
You can use SimpleDateFormat class.
Like this:
String dateString = "2014-10-13 18:22:54.71";
SimpleDateFormat format =
new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.S");
try {
Date parsed = format.parse(dateString);
}
catch(ParseException pe) {
System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
}
Related
i have tried most things online from custom calendar views to dependencies but they all lead to being outdated and not usable for android studio in its latest version.
does anyone know how to achieve this? I have tried mCalendarView, SunDeepK CalendarView and material-calendar view, but to no avail..
private void setCustomResourceForDates() {
Calendar cal = Calendar.getInstance();
//highlighlighting the holidays in a month taking the static dates
ArrayList<String> dates = new ArrayList<String>();
dates.add("02-08-2015");
dates.add("22-08-2015");
dates.add("17-09-2015");
dates.add("25-09-2015");
dates.add("27-09-2015");
dates.add("13-10-2015");
dates.add("22-10-2015");
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
for (int i = 1; i < dates.size(); i++) {
inputString2 = dates.get(i);
inputString1 = myFormat.format(date);
try {
//Converting String format to date format
date1 = myFormat.parse(inputString1);
date2 = myFormat.parse(inputString2);
//Calculating number of days from two dates
long diff = date2.getTime() - date1.getTime();
long datee = diff / (1000 * 60 * 60 * 24);
//Converting long type to int type
day = (int) datee;
} catch (ParseException e) {
e.printStackTrace();
}
cal = Calendar.getInstance();
cal.add(Calendar.DATE, day);
holidayDay = cal.getTime();
colors();
}
}
public void colors() {
if (caldroidFragment != null) {
caldroidFragment.setBackgroundResourceForDate(R.color.green,
holidayDay);
caldroidFragment.setTextColorForDate(R.color.white, holidayDay);
}
}
}
call setCustomResourceForDates(); on onCreate method (in Caldroid Calendar
you can find it here : https://stackoverflow.com/a/32601769/20137896
This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
I'm working on an android application and new to it.
I have to get date from user and then add 28 days and store it in database.
This is what I have done so far
private void saveDate() throws ParseException {
DatabaseHelper db = new DatabaseHelper(ActivityPeriodToday.this.getActivity());
String pDate = periodDate.getText().toString().trim();
String pTime = periodTime.getText().toString().trim();
String next_expected = getNextExpected(pDate);
boolean isInserted = db.insertPeriodTodayIntoPeriods(pDate, pTime, early_late, pDifference, pType, next_expected);
if (isInserted == true) {
Toast.makeText(ActivityPeriodToday.this.getActivity(), "Saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ActivityPeriodToday.this.getActivity(), "Could not be saved", Toast.LENGTH_SHORT).show();
}
}
private String getNextExpected(String pDate) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(pDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, 28);
return sdf.format(c.getTime());
}
But is code is not incrementing month.
Ex. If user selects 01/11/2016, then date is incremented and is saved
29/11/2016. But if user selects 16/11/2016 then saves date is
28/11/2016 but this should be 14/12/2016
Step 1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
Step-2 use add() to add number of days to calendar
c.add(Calendar.DATE, 40);
Try using this:
calendar.add(Calendar.DAY_OF_YEAR, 28);
Its Working for me.
Calendar c = Calendar.getInstance();
int Year = c.get(Calendar.YEAR);
int Month = c.get(Calendar.MONTH);
int Day = c.get(Calendar.DAY_OF_MONTH);
// current date
String CurrentDate = Year + "/" + Month + "/" + Day;
String dateInString = CurrentDate; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dateInString));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 28);//insert the number of days that you want
sdf = new SimpleDateFormat("dd/MM/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
Toast.makeText(MainActivity.this, ""+dateInString, Toast.LENGTH_SHORT).show();
Your question may already have an answer here: How can I increment a date by one day in Java?
Or you can simply use
c.add(Calendar.DATE, 28);
instead of
c.add(Calendar.DAY_OF_MONTH, 28);
This question already has answers here:
Adding n hours to a date in Java?
(16 answers)
Closed 7 years ago.
I have a TextView that store a getDate() value. This getDate() value is a date but the format is String
textview_device_datetime.setText(data.getDate().replace('T', ' '));
this is the result
16-08-2015 16:15:16
but i would add 2 hours to this String Date.
How can i do?
Any help is great.
Thanks
final String dateString = "16-08-2015 16:15:16";
final long millisToAdd = 7_200_000; //two hours
DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date d = format.parse(dateString);
d.setTime(d.getTime() + millisToAdd);
System.out.println("New value: " + d); //New value: Sun Aug 16 18:15:16 CEST 2015
Here I have attached the code for it with example.
import java.time.*;
import java.text.*;
import java.util.*;
public class AddTime
{
public static void main(String[] args)
{
String myTime = "16-08-2015 16:15:16";
System.out.println(addHour(myTime,2));
}
public static String addHour(String myTime,int number)
{
try
{
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date d = df.parse(myTime);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.HOUR, number);
String newTime = df.format(cal.getTime());
return newTime;
}
catch(ParseException e)
{
System.out.println(" Parsing Exception");
}
return null;
}
}
This question already has answers here:
How to round time to the nearest quarter hour in java?
(17 answers)
Closed 8 years ago.
I have the following code to get datetime UTC.
public static Date GetUTCdatetimeAsDate()
{
return StringDateToDate(GetUTCdatetimeAsString());
}
public static String GetUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
return utcTime;
}
public static Date StringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
Now I want to use GETUTCdatetimeAsDate and if I get for example 11/22/2014 03:12 the minutes will be rounded up to the nearest 5 minutes. So in this case that will be 11/22/2014 03:15. If it is 11/22/2014 03:31 it will be 11/22/2014 03:35.
Any ideas on how to achieve this?
public static Date StringDateToDate(String StrDate) {
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
try {
dateToReturn = (Date) dateFormat.parse(StrDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(dateToReturn);
int minute = c.get(Calendar.MINUTE);
minute = minute % 5;
if (minute != 0) {
int minuteToAdd = 5 - minute;
c.add(Calendar.MINUTE, minuteToAdd);
}
return c.getTime();
}
I use a PostgreSQL database server. One column has "timestamp with time zone" and I upload time string through XML like '2012-07-11 04:45:18+GMT+05:30' but it accepts this format '2012-02-16 19:47:22.652+05:30'
So, how can I format the time string on Android? Below is my function.
Calendar c=Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ssZ");
Log.v("AKDN", "Time string is:"+formatter.format(c.getTime()));
return formatter.format(c.getTime());
This works fine for me:
private String getTime()
{
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SZ");
return formatter.format(c.getTime());
}
In my case it outputs:
2012-07-11 06:32:46.118-0600
EDIT:
You can either add the ":" to your string.
Or you can create another function to format the timezone. Something like this:
private String formatTimeZone(TimeZone tz)
{
DecimalFormat format = new DecimalFormat("00");
String timeZone = new String("");
int offset = tz.getRawOffset();
if(offset != 0)
{
int hours = Math.abs((offset / (60 * 1000)) / 60);
int minutes = Math.abs((offset / (60 * 1000)) % 60);
timeZone += offset < 0 ? "-" : "+";
timeZone += format.format(hours);
timeZone += ":";
timeZone += format.format(minutes);
}
return timeZone;
}