java


How to set date and time 6 hour before to picked time and date?


I am developing an app, here I want to use Date and time picker, To pick a date and time but I want to set result time 6 hours before picked time. How do I set this time 6 hour before the pick time with appropriate date with respect to time?
Example:
if i am pick date suppose "30sep" and time is "1:00am" then date and time I would like to display will be "29sep" and "7:00pm".
How do I do?
I am using following code:
#Override
public void onClick(View v) {
if (v == pdate) {
// Get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
eddate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == ptime) {
// Get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
edtime.setText(hourOfDay + ":" + minute);
if(hourOfDay==00)
{
tvpicktime.setText((17) + ":" + minute);
}
else if(hourOfDay==1)
{
tvpicktime.setText((18) + ":" + minute );
}
else if(hourOfDay==2)
{
tvpicktime.setText((19) + ":" + minute );
}
else if(hourOfDay==3)
{
tvpicktime.setText((20) + ":" + minute );
}
else if(hourOfDay==4)
{
tvpicktime.setText((21) + ":" + minute);
}
else if(hourOfDay==5)
{
tvpicktime.setText((22) + ":" + minute );
}
else if(hourOfDay==6)
{
tvpicktime.setText((23) + ":" + minute);
}
else if(hourOfDay==7)
{
tvpicktime.setText((00) + ":" + minute);
}
else if(hourOfDay==8)
{
tvpicktime.setText((01) + ":" + minute);
}
else if(hourOfDay==9)
{
tvpicktime.setText((02) + ":" + minute);
}
else if(hourOfDay==10)
{
tvpicktime.setText((03) + ":" + minute);
}
else if(hourOfDay==11)
{
tvpicktime.setText((04) + ":" + minute);
}
else if(hourOfDay==12)
{
tvpicktime.setText((05) + ":" + minute);
}
if(hourOfDay==13)
{
tvpicktime.setText((06) + ":" + minute);
}
else if(hourOfDay==14)
{
tvpicktime.setText((07) + ":" + minute );
}
else if(hourOfDay==15)
{
tvpicktime.setText((8) + ":" + minute );
}
else if(hourOfDay==16)
{
tvpicktime.setText((9) + ":" + minute);
}
else if(hourOfDay==17)
{
tvpicktime.setText((10) + ":" + minute );
}
else if(hourOfDay==18)
{
tvpicktime.setText((11) + ":" + minute);
}
else if(hourOfDay==19)
{
tvpicktime.setText((12) + ":" + minute);
}
else if(hourOfDay==20)
{
tvpicktime.setText((13) + ":" + minute);
}
else if(hourOfDay==21)
{
tvpicktime.setText((14) + ":" + minute);
}
else if(hourOfDay==22)
{
tvpicktime.setText((15) + ":" + minute);
}
else if(hourOfDay==23)
{
tvpicktime.setText((16) + ":" + minute);
}
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
In your code you are removing 7 hours and you are not changing the day (if it's 1 AM, you should go in the previous day, but anyway you can simply call this instead of your switch statement:
c.add(Calendar.HOUR_OF_DAY, -6);
This will add -6 hours so it will remove 6 hours
To show result in a textfield
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");
myTextField.setText(simpleDate.format(c.getTime()));

Related Links

ResultSet.next() row counter fails to count
What is the best practice to put Velocity properties when using VelocityViewServlet
Retrieving all links within an ordered list using XPath
GELFAppender exception in weblogic JMS
Some print functions do not run in this calculation class. Why? [closed]
Hibernate second level cache cleared
Storing Objects of Class into an Array
How to start testing a RESTful web service using Java ? [duplicate]
kafka consumer to dynamically detect topics added
RMI Client/Sever - File Transfer
Usage of super in method declaration
How to save ID passed by activity (android) [closed]
Re-sizing an image which in Base64 format and converting to Base64 again in java
How to abstract a binary search that is not over a data structure?
ProgressDialog shows before 2nd TabBar fragment is opened
TCP Server & Client : IOException raised when Server respond to Client

Categories

HOME
yii2
urbancode
image-processing
gremlin
mockito
session
iot
framework7
frameworks
cakephp-2.5
uber-api
webrequest
kentor-authservices
collectd
alignment
n-gram
gz
leiningen
windows-server-2012
decimal
lombok
libtiff
pc
invantive-sql
apache-metamodel
connection-string
jprofiler
predix
scriptcs
errorlevel
restful-authentication
crystal-reports-2010
emgucv
info.plist
traffic
windows-error-reporting
kendo-datasource
madlib
framemaker
openoffice.org
devextreme
dosbox
qwerty
c11
mesos-chronos
firebase-admin
nomethoderror
password-encryption
qwt
auto-update
logparser
pnotify
idisposable
media-player
account-kit
time-and-attendance
visual-c++-2008
url-pattern
merge-conflict-resolution
xml-attribute
multiple-regression
php-parse-error
pyke
browser-link
ios8-today-widget
teamcity-8.0
eclipse-clp
dundas
key-management
sonarqube5.1.2
bigbluebutton
dia
website-monitoring
dd
issuu
message-driven-bean
fmod
.aspxauth
qt-faststart
sabredav
flashvars
pvrtc
datawindow
parametric-equations
work-stealing
custom-backend
subgurim-maps
appender
iphone-web-app
coda-slider
ncqrs
procedural-music
gacutil
ugc
windows-live-messenger
ajax-forms

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App