Guest User

Untitled

a guest
Apr 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. package de.test.trafficlightcountdown;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.NumberFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.Locale;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. import android.app.Activity;
  11. import android.net.ParseException;
  12. import android.os.Bundle;
  13. import android.os.CountDownTimer;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import de.werneritsolutions.trafficlightcountdown.R;
  19.  
  20. public class TrafficLightCountdownActivity extends Activity
  21. {
  22. TextView day,hour,min,sec;
  23. int iDay,iHour,iMin,iSec;
  24. MyCount counter;
  25. Date endDate = null;
  26. Date startDate = null;
  27. NumberFormat myFormat = NumberFormat.getInstance();
  28.  
  29. @Override
  30. public void onCreate(Bundle savedInstanceState)
  31. {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. final SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");
  35. day=(TextView)findViewById(R.id.day);
  36. hour=(TextView)findViewById(R.id.hour);
  37. min=(TextView)findViewById(R.id.min);
  38. sec=(TextView)findViewById(R.id.sec);
  39. myFormat.setMinimumIntegerDigits(2);
  40.  
  41. Date now = new Date();
  42. final String currentDate = outputFormat.format(now);
  43. String nextDate = null;
  44. try
  45. {
  46. nextDate = eventFinder(now);
  47. }
  48. catch (Exception e1)
  49. {
  50. e1.printStackTrace();
  51. }
  52.  
  53. Button btnStart=(Button)findViewById(R.id.btnstart);
  54.  
  55. btnStart.setOnClickListener(new View.OnClickListener()
  56. {
  57. public void onClick(View arg0)
  58. {
  59. try
  60. {
  61. try
  62. {
  63. endDate = outputFormat.parse(nextDate);
  64. }
  65. catch (java.text.ParseException e)
  66. {
  67. e.printStackTrace();
  68. }
  69. try
  70. {
  71. startDate=outputFormat.parse(currentDate);
  72. }
  73. catch (java.text.ParseException e)
  74. {
  75. e.printStackTrace();
  76. }
  77. long diffInMis= endDate.getTime() - startDate.getTime();
  78. if(diffInMis<0)
  79. {
  80. Toast.makeText(getBaseContext(), "Please, Enter valid Time...",Toast.LENGTH_SHORT).show();
  81. }
  82. else
  83. {
  84. long diff = TimeUnit.MILLISECONDS.toSeconds(diffInMis);
  85. iDay=(int) (diff/(60*60*24));
  86. long lday= (diff%(60*60*24));
  87. iHour=(int)(lday/3600);
  88. long lhour= (lday%(60*60));
  89. iMin=(int)(lhour/60);
  90. long lmin= (lhour%(60));
  91. iSec=(int)(lmin);
  92. hour.setText(String.valueOf(myFormat.format(iHour)).toString());
  93. min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
  94. sec.setText(":"+String.valueOf(myFormat.format(iSec)).toString());
  95. counter = new MyCount(iSec*1000,1000);
  96. counter.start();
  97. }
  98. }
  99. catch (ParseException e)
  100. {
  101. e.printStackTrace();
  102. }
  103. };
  104. });
  105.  
  106. Button btnStop=(Button)findViewById(R.id.btnstop);
  107. btnStop.setOnClickListener(new View.OnClickListener()
  108. {
  109. public void onClick(View arg0)
  110. {
  111. counter.cancel();
  112. hour.setText("00");
  113. min.setText(":"+"00");
  114. sec.setText(":"+"00");
  115. }
  116. });
  117. }
  118.  
  119. static public String eventFinder(Date cdt) throws Exception
  120. {
  121. Date currentDateTime = cdt;
  122.  
  123. String[] eventTimes = {
  124. "22:15:41",
  125. "22:23:07",
  126. "22:30:33",
  127. "22:37:59",
  128. "22:45:25",
  129. "22:52:51",
  130. "23:00:17",
  131. "23:07:43",
  132. "23:15:09",
  133. "23:24:35",
  134. "23:30:01",
  135. "23:37:27",
  136. "23:44:53"
  137. };
  138.  
  139. boolean foundMatch = false;
  140. long diff = -1;
  141. String timeToKnow = null;
  142.  
  143. for(int index = 0; index < eventTimes.length; index++)
  144. {
  145. diff = getDateDiffMillis(currentDateTime, currentDateTime, eventTimes[index]);
  146.  
  147. if(diff > 0)
  148. {
  149. foundMatch = true;
  150. timeToKnow = eventTimes[index];
  151. break;
  152. }
  153. }
  154.  
  155. if(!foundMatch)
  156. {
  157. Date tomorrowDateTime = new Date(new Date().getTime() + 86400000L); //+24h als Millisekunden
  158. diff = getDateDiffMillis(currentDateTime, tomorrowDateTime, eventTimes[0]);
  159. timeToKnow = eventTimes[0];
  160. }
  161.  
  162. System.out.println(timeToKnow);
  163. return timeToKnow;
  164. }
  165.  
  166. static public long getDateDiffMillis(Date dateTime, Date eventDate, String eventTime) throws Exception
  167. {
  168. long diff = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.GERMANY).parse(DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMANY).format(eventDate) +" "+ eventTime).getTime() - dateTime.getTime();
  169. return diff;
  170. }
  171.  
  172. public class MyCount extends CountDownTimer
  173. {
  174. public MyCount(long millisInFuture, long countDownInterval)
  175. {
  176. super(millisInFuture, countDownInterval);
  177. }
  178. @Override
  179. public void onFinish()
  180. {
  181. counter = new MyCount(60000,1000);
  182. counter.start();
  183. iMin-=1;
  184. if(iMin>-1)
  185. {
  186. min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
  187. }
  188. else
  189. {
  190. iMin=59;
  191. min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
  192. iHour-=1;
  193. if(iHour>-1)
  194. {
  195. hour.setText(String.valueOf(myFormat.format(iHour)).toString());
  196. }
  197. else
  198. {
  199. iHour=11;
  200. hour.setText(String.valueOf(myFormat.format(iHour)).toString());
  201. iDay-=1;
  202. if(iDay>-1)
  203. {
  204. day.setText(" "+String.valueOf(iDay).toString());
  205. }
  206. else
  207. {
  208. day.setText("Green!");
  209. hour.setText("");
  210. min.setText("");
  211. sec.setText("");
  212. counter.cancel();
  213. }
  214. }
  215. }
  216. }
  217.  
  218. @Override
  219. public void onTick(long millisUntilFinished)
  220. {
  221. sec.setText(":"+String.valueOf(myFormat.format(millisUntilFinished/1000)));
  222. }
  223. }
  224. }
Add Comment
Please, Sign In to add comment