How do I detect duplicate strings in android? - android

My code saves a string from edit text every time I press a button. But If I type the same string again, It would save it regardless. How do I prevent it from saving if it has been saved before already?
HistoryHelper file = new HistoryHelper(getBaseContext());
// get date
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DATE);
int month = c.get(Calendar.MONTH) + 1;// fixes wrong month
int year = c.get(Calendar.YEAR);
file.writeToSD("Date: " + year + "-" + month + "-" + date);
// get string
file.writeToSD(NEWLINK.toString());
// leave a blank line space
file.writeToSD("".toString());

Related

NumberFormatException when setting SimpleDateFormat

The date from DateRangePicker displays as: "yyyy-M-d".
But I want it to display the date as: "yyyy-MM-dd".
I have tried out the following code:
#Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth, int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {
String selection1 = year + "-" + (monthOfYear+1) + "-" + dayOfMonth;
String selection2 = yearEnd + "-" + (monthOfYearEnd+1) + "-" + dayOfMonthEnd;
Long firstDateSelection = Long.parseLong(selection1);
Long secondDateSelection = Long.parseLong(selection2);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = new Date(firstDateSelection);
Date endDate = new Date(secondDateSelection);
formatter.format(startDate); formatter.format(endDate);
But I get NumberFromatException when I run it at "Long firstDateSelection = Long.parseLong(selection1)"?
Help is much appreciated :)
Thanks!
You are getting NumberFormatException because "1234-12-23" is not a valid Long number and you are trying to convert it to a Long here:
Long firstDateSelection = Long.parseLong(selection1);
Since you already have the numbers for day, month and year you can simply format it(no need for SimpleDateFormat):
String dateString = String.format("%1$04d-%2$02d-%3$02d", year, monthOfYear, dayOfMonth);
If you check Javadoc about parseLong(string)
Throws
NumberFormatException
if the string does not contain a parsable long.
As you can see a string with "-" is not a long number and it throws that exception.
If you want to do it correctly you have to create a Calendar, add it the values you are passing by parameters (year, monthOfYear...) with set(...) and then make getTime() to get Date object.
Don't create a Date object directly (new Date...) because its methods setHour, etc. are deprecated.
Once you have that, you will be able to do .format(dateObject).

Convert Received date time to local timezone time Android

Good day.I am building an chat application.For purpose i decided to put a date of message had been sent.No wounder that in different countries the date must show different.For example let's take 2 different countries and assume the difference between them are 2 hours.CountryX and CountryY.The user send message from CountryX which time is lets say 15:00.I am saving this on server with exact timezone time as user sent,exactly 15:00 as CountryX.Second user receives the message in CountryY which is more in 2 hours from CountryX,so basically The time which I MUST show in CountryY must be 17:00.This is the issue.How can i convert an already received time with known timezone to local in order to show correct?I did google a lot but all i came up,were solutions where you would simply just get an time for exact country,and not convert the CountryX sent Time to CountryY local time to show it correctly in CountryY.Please can you provide an help?Thank you very much beforehand.
For everyone suffering because of this.I have ended up creating my own class with my own logic and it works flawlessly and as an extra bonus couple of handy methods with time
public class Time {
public static String getCurrentTime() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String finalFormat = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
if (month < 10) {
String finalMonth = "0" + month;
finalFormat = year + "-" + finalMonth + "-" + day + " " + hour + ":" + minute + ":" + second;
}
return finalFormat;
}
public static String convertToLocalTime(String timeToConvert) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone(Constants.SERVER_TIME_ZONE));//where server time zone is the time zone of your server as defauul,E.X -America/Los_Angeles
Date parsed;
try {
parsed = sourceFormat.parse(timeToConvert);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
return result;
}
public static String getTimeZone() {
return TimeZone.getDefault().getID();
}
}

Trying to show to chosen day as text after the DatePicker

//When user clicks "other".
public void setDate(View view) {
DateTime dateTime=new DateTime();
new DatePickerDialog(CreateEventActivity.this, listener, dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfWeek()).show();
}
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
DateTime eventDate = new DateTime();
eventDate.withYear(year);
eventDate.withMonthOfYear(monthOfYear + 1);
eventDate.withDayOfMonth(dayOfMonth);
time = eventDate;
Context appContext = getApplicationContext();
Toast.makeText(appContext, dayOfMonth + "/" + (monthOfYear + 1) + "/" + year + "," +eventDate.dayOfWeek().getAsText(), Toast.LENGTH_LONG).show();
dateTxt.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
dayTxt.setText(eventDate.dayOfWeek().getAsText());
otherButton.setBackgroundColor(Color.parseColor("#77B3FC"));
todayButton.setBackgroundColor(Color.parseColor("#DBDBDB"));
tomorrowButton.setBackgroundColor(Color.parseColor("#DBDBDB"));
}
};
I have a problem with this part: eventDate.dayOfWeek().getAsText()
It shows the wrong day of the week, but the date in numbers is fine.
(Just started using Joda-Time so I'm not sure about it...)
EDIT:
The date in numbers like: 23.7.16 is printed correctly, But I want it to show which day is it in the week, like "Monday"... I've noticed it always writes today's name. In the toast and in the text view...
For example, for couple of different dates it will show:
23.6.16, Friday|
15.7.16, Friday|
30.8.17, Friday
the others data are right because you are using the data of the DataPicker and not of the JodaTime, may have some error in your construction try:
DateTime eventDate = new DateTime(year,monthOfYear+1,dayOfMonth,0,0,0);
the 0,0,0 are hours, minutes and seconds, remove the 3 lines :
eventDate.withYear(year);
eventDate.withMonthOfYear(monthOfYear + 1);
eventDate.withDayOfMonth(dayOfMonth);

Android how to customize date in listview

I want to show the date from current date on words in list view .How to customize that date.Could please any one tell me.For example when u booking ticket in railways it's showing the current date on-wards.Like that i want to implement.
![enter image description here][1]
[1]: http://i.stack.imgur.com/3NPXA.png
use the SimpleDateFormat for example
Date d = new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("c - LLLL");
String formattedDate = simpleDateFormat.format(d);//Mon - June for that format
Check the formats (yyyy,dd ...) in SimpleDateFormat
Hope this will help.
Code for checking date after or before today.
Calendar c = Calendar.getInstance();
// set the calendar to start of today
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
// and get that as a Date
Date today = c.getTime();
// or as a timestamp in milliseconds
long todayInMillis = c.getTimeInMillis();
// user-specified date which you are testing
// let's say the components come from a form or something
int year = 2011;
int month = 5;
int dayOfMonth = 20;
// reuse the calendar to set user specified date
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// and get that as a Date
Date dateSpecified = c.getTime();
// test your condition
if (dateSpecified.before(today)) {
System.err.println("Date specified [" + dateSpecified + "] is before today [" + today + "]");
} else {
System.err.println("Date specified [" + dateSpecified + "] is NOT before today [" + today + "]");
}
Done

How can I show today's date in an android activity?

I am currently working on my project for my professional course. I am implementing a digital diary.
I would be having a diary page (a multiline textbox maybe) And a TextView on top to show the current DATE (without time). How do I show a NON-editabale textbox with today's date shown in it.
In order to get the current date and time use the below Links and iin order make it non editable in XML file make editable = false also focusable = false.
LINK1
LINK2
its simple
Date dt = new Date();
int date = dt.getDate();
int month = dt.getMonth()+1;
int year = dt.getYear();
year += 1900;
int day = dt.getDay();
String[] days = { "Sunday", "Monday", "Tuesday","WednesDay", "Thursday", "Friday", "Saterday" };
String curDate = date + "/" + month + "/" + year + " " + days[day];
Use this code :
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
It 'll print :
Feb 5, 2013, 12:35:46PM

Categories

Resources