How to subtract minutes from current time in java



Program #1 : Java Example Program to subtract minutes from given string formatted date time using java 8.


  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract minutes from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubtractMinutesJava{

  11. public static void main(String[] args) {
  12.  
  13.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  14.  
  15.         String currentTime= "2017-10-19 22:00:00";

  16.  
  17.         System.out.println("Before subtraction of minutes from date: "+currentTime);
  18.  
  19.         LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  20.  
  21.         datetime=datetime.minusMinutes(30);
  22.  
  23.         String aftersubtraction=datetime.format(formatter);
  24.         System.out.println("After 30 minutes subtraction from date: "+aftersubtraction);

  25.  
  26.     }
  27.  
  28. }

Output:

  1. Before subtraction of minutes from date: 2017-10-19 22:00:00
  2. After 30 minutes subtraction from date: 2017-10-19 21:30:00

Program #2 : Java Example Program to subtract seconds from given string formatted date time using java 8.
  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract seconds from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubtractSecondsJava{

  11. public static void main(String[] args) {
  12.  
  13.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  14.  
  15.         String currentTime= "2017-10-19 22:00:00";

  16.  
  17.         System.out.println("Before subtraction of seconds from date time: "+currentTime);
  18.  
  19.         LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  20.  
  21.         datetime=datetime.minusSeconds(30);
  22.  
  23.         String aftersubtraction=datetime.format(formatter);
  24.         System.out.println("After 30 seconds subtraction from date time: "+aftersubtraction);

  25.  
  26.     }
  27.  
  28. }

Output:

  1. Before subtraction of seconds from date time: 2017-10-19 22:00:00
  2. After 30 seconds subtraction from date time: 2017-10-19 21:59:30

Program #3 : Java Example Program to subtract seconds from current date time using java 8.


how to subtract minutes from java date time

How to subtract N hours from current date time using java 8

  • We have already seen how to subtract number of days from java using calendar and using java 8.
  • How to subtract X days from a date using Java calendar and with java 8 
  • Now we will discuss here how to subtract hours from java 8 localdatetime.
  • Some times we will get out date time in string format so we will convert our string format date to java 8 LocaDateTime object.
  • We can user DateTimeFormatter to tell the format of date time to LocalDateTime class.
  • LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  • LocalDateTime has minusHours(numberOfHours) method to subtract hours from date time.
  • Lets see an example java program to subtract hours from java date time using java 8 LocalDateTime class.
  • How to subtract hours from java 8 date time.



Program #1 : Java Example Program to subtract hours from given string formatted date time using java 8.



  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract hours from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubstractHoursJava{
  11. public static void main(String[] args) {
  12.  
  13.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  14.  
  15.         String currentTime= "2017-10-19 22:00:00";
  16.  
  17.         System.out.println("Before subtraction of hours from date: "+currentTime);
  18.  
  19.         LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
  20.  
  21.         datetime=datetime.minusHours(4);
  22.  
  23.         String aftersubtraction=datetime.format(formatter);
  24.         System.out.println("After 4 hours subtraction from date: "+aftersubtraction);
  25.  
  26.     }
  27.  
  28. }

Output:

  1. Before subtraction of hours from date: 2017-10-19 22:00:00
  2. After 4 hours subtraction from date: 2017-10-19 18:00:00

  • Now we will see how to subtract one hour from current date time in java using java 8 

Program #2 : Java Example Program to subtract one hour from given current date time using java 8.

  1. package java8;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: how to subtract hours from date time using java 8  
  5. *
  6.  */
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9.  
  10. public class SubtractOneHour {
  11.  
  12. public static void main(String[] args) {
  13.  
  14.   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss");
  15.  
  16.         LocalDateTime datetime = LocalDateTime.now();
  17.         System.out.println("Before subtraction of hours from date: "+datetime.format(formatter));
  18.  
  19.        datetime=datetime.minusHours(1);
  20.        String aftersubtraction=datetime.format(formatter);
  21.        System.out.println("After 1 hour subtraction from date: "+aftersubtraction);
  22.  
  23.     }
  24.  
  25. }
Output:


  1. Before subtraction of hours from date: 2017-10-19 23:14:12
  2. After 1 hour subtraction from date: 2017-10-19 22:14:12

subtract hours from java date time

C program to insert an element in an array

  • Program to insert an element in an array at a given position
  • C program to insert an element in an array without using function



Program #1: Write a c program to insert an element in array without using function


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int insarray[100], pos, c, n, value;
  7.  
  8.   printf("Enter number of elements of array\n");
  9.    scanf("%d", &n);
  10.  
  11.    printf("Enter all %d elements\n", n);
  12.  
  13.    for (c = 0; c < n; c++)
  14.       scanf("%d", &insarray[c]);
  15.  
  16.    printf("Enter the specific position where you wish to insert an element in array\n");
  17.    scanf("%d", &pos);
  18.  
  19.    printf("Enter the value to insert\n");
  20.    scanf("%d", &value);
  21.  
  22.    for (c = n - 1; c >= pos - 1; c--)
  23.       insarray[c+1] = insarray[c];
  24.  
  25.    insarray[pos-1] = value;
  26.  
  27.    printf("Resultant array is\n");
  28.  
  29.    for (c = 0; c <= n; c++)
  30.       printf("%d\n", insarray[c]);
  31.  
  32. getch();
  33. }
Output:

insert element in array c

C program to delete an element in an array

  • Write a c program to insert and delete an element in array
  • Write a c program to delete an element in an array
  • C Program to Delete the Specified Integer from an Array without using function



Program #1: Write a c program to insert and delete an element in array


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   
  7.      int array[10];
  8.  
  9.     int i, n, position, element, exist = 0;
  10.  
  11.  
  12.     printf("Enter number of elements \n");
  13.  
  14.     scanf("%d", &n);
  15.  
  16.     printf("Enter the elements\n");
  17.  
  18.     for (i = 0; i < n; i++)
  19.  
  20.     {
  21.  
  22.         scanf("%d", &array[i]);
  23.  
  24.     }
  25.  
  26.     printf("Input array elements are\n");
  27.  
  28.     for (i = 0; i < n; i++)
  29.  
  30.     {
  31.  
  32.         printf("%d\n", array[i]);
  33.  
  34.     }
  35.  
  36.     printf("Enter the element to be deleted from array\n");
  37.  
  38.     scanf("%d", &element);
  39.  
  40.     for (i = 0; i < n; i++)
  41.  
  42.     {
  43.  
  44.         if (array[i] == element)
  45.  
  46.         {
  47.             exist = 1;
  48.  
  49.             position = i;
  50.  
  51.             break;
  52.  
  53.         }
  54.  
  55.     }
  56.  
  57.     if (exist == 1)
  58.  
  59.     {
  60.  
  61.         for (i = position; i <  n - 1; i++)
  62.  
  63.         {
  64.  
  65.             array[i] = array[i + 1];
  66.  
  67.         }
  68.  
  69.         printf("array after deletion  \n");
  70.  
  71.         for (i = 0; i < n - 1; i++)
  72.  
  73.         {
  74.  
  75.             printf("%d\n", array[i]);
  76.  
  77.         }
  78.  
  79.     }
  80.  
  81.     else
  82.  
  83.         printf("Given element %d is not exist in the array \n", element);
  84. getch();
  85. }
Output:


delete array element c

Apache Spark create rdd from an array java | convert an array in to RDD

  • RDD is the spark's core abstraction.
  • Full form of RDD is resilient distributed dataset.
  • Each RDD will split into multiple partitions which may be computed in different machines of cluster
  • We can create Apache spark RDD in two ways
    1. Parallelizing a collection
    2. Loading an external dataset.
     
  • So Creating RDD from an array comes in under Parallelizing a collection.
  • Let us see Apache spark an example program to convert an array into RDD.



Program #1: Write a Apache spark java example program to create simple RDD using parallelize method of JavaSparkContext. convert an array in to RDD


  1.  package com.instanceofjava.sparkInterview;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.apache.spark.SparkConf;
  6. import org.apache.spark.api.java.JavaRDD;
  7. import org.apache.spark.api.java.JavaSparkContext;
  8.  
  9. /**
  10.  *  Apache spark examples:RDD in spark example program  
  11. *  converting an array to RDD
  12.  * @author www.instanceofjava.com
  13.  */
  14. public class SparkTest {
  15.     
  16.     public static void main(String[] args) {
  17.         
  18.         SparkConf conf = new
  19. SparkConf().setMaster("local[2]").setAppName("InstanceofjavaAPP");
  20.         JavaSparkContext sc = new JavaSparkContext(conf);
  21.         
  22.         String[] arrayStr={"convert array to rdd","convert array into rdd"};
  23.         
  24.         JavaRDD<String> strRdd=sc.parallelize(Arrays.asList(arrayStr));
  25.         System.out.println("apache spark rdd created: "+strRdd);
  26.         
  27.         /**
  28.          * Return the first element in this RDD.
  29.          */
  30.         System.out.println(strRdd.first());
  31.         
  32.     }
  33.  
  34. }
  35. }

Output:

  1. apache spark rdd created: ParallelCollectionRDD[0] at parallelize at SparkTest.java:24
  2. convert array to rdd



convert an array to rdd

How to create rdd in apache spark using java

  • RDD is the spark's core abstraction.
  • Full form of RDD is resilient distributed dataset.
  • That means it is immutable collection of objects.
  • Each RDD will split into multiple partitions which may be computed in different machines of cluster
  • We can create Apache spark RDD in two ways
    1. Parallelizing a collection
    2. Loading an external dataset.
  • Now we sill see an example program on creating RDD by parallelizing a collection.
  • In Apache spark JavaSparkContext  class providing parallelize() method.
  • Let us see the simple example program to create Apache spark RDD in java



Program #1: Write a Apache spark java example program to create simple RDD using parallelize method of JavaSparkContext.

  1. package com.instanceofjava.sparkInterview;
  2. import java.util.Arrays;
  3.  
  4. import org.apache.spark.SparkConf;
  5. import org.apache.spark.api.java.JavaRDD;
  6. import org.apache.spark.api.java.JavaSparkContext;
  7.  
  8. /**
  9.  *  Apache spark examples:RDD in spark example program
  10.  * @author www.instanceofjava.com
  11.  */
  12. public class SparkTest {
  13.     
  14.  public static void main(String[] args) {
  15.         
  16.  SparkConf conf = new SparkConf().setMaster("local[2]").setAppName("InstanceofjavaAPP");
  17.  JavaSparkContext sc = new JavaSparkContext(conf);
  18.         
  19.  JavaRDD<String> strRdd=sc.parallelize(Arrays.asList("apache spark element1","apache
  20. spark element2"));
  21.  System.out.println("apache spark rdd created: "+strRdd);
  22.         
  23. /**
  24.  * Return the first element in this RDD.
  25.  */
  26. System.out.println(strRdd.first());
  27.  }
  28.  
  29. }
  30.  
  31. }

Output:

  1. apache spark rdd created: ParallelCollectionRDD[0] at parallelize at SparkTest.java:21
  2. spark element1


create apache spark rdd java
Select Menu