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

Overriding equals method to accept doubles inside of Object class. Java
Line break between GWT widgets
How to set shift in String.format dynamically? [duplicate]
Java audio doesn't play in jar file
How can I obtain the -D parameters passed in to Java launch
Set multiple listeners to one method
why one class of same package can't access another?
cannot find symbol : class NumberTools
Java apache POI java.lang.IllegalArgumentException: Position 21504 past the end of the file
Selenium Automatically Closes Chrome Console (attached or window)
How to check values in collection for uniques by some criteria using Java 8
java.util.concurrent.locks.Condition awaitUninterruptibly()
what is the purpose of this tmp variable in android sample bluetooth chat application
ASM. Clone JumpInsnNode correctly
codenameone is there a simpler/nicer way to call a Web Service?
Spark job-server

Categories

HOME
gitlab
include
react-router
youtube-dl
adb
sqlite-net-extensions
amortized-analysis
sql-server-2016
disassembler
azure-storage-tables
synchronization
maude-system
ssl-client-authentication
qt-creator
visual-studio-2005
decimal
ghc
jqwidget
dbext
kryo
kvc
android-widget
sparse-matrix
entitlements
maxmind
elasticsearch-ruby
gitignore
unboundid
directx-10
functor
copying
asset-pipeline
subset-sum
language-concepts
qwerty
uft-api
splice
cookiecutter-django
galen
sql-server-agent
botbuilder
errordocument
datastax-startup
np-complete
pdfclown
blacklist
chord-diagram
messenger
time-and-attendance
url-pattern
android-textview
pcf
django-debug-toolbar
websitepanel
angstrom-linux
system.management
netmq
natvis
lib.web.mvc
teamcity-8.0
t4mvc
cyclomatic-complexity
sframe
service-accounts
php-ci
sailfish-os
unity3d-gui
dereference
javax.mail
npapi
xc16
java.util.concurrent
jquery-layout
rebol3
rtmfp
.aspxauth
jmapviewer
dbconnection
dataservice
eclipse-memory-analyzer
gridcontrol
pushbackinputstream
plasma
android-screen-support
cassini-dev
nsnetservice
reddot
work-stealing
adk
gnustep
stage
sortable-tables
dbal
webkit.net
mediarss

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