Delta_G has presented you with one alternative. There are always others.
First, the String class is not very efficient in terms of memory usage. Your program uses 6596 bytes. By dropping the String class and using a char array, it drops to 5330 bytes.
Second, if the first test:
if ((curMillis - prevMillis) == 1000) {
is logic True, your code proceeds to make 4 more tests that can't possibly be true. A cascading if statement block is a common solution to this inefficiency. Delta_G's solution uses this.
However, you can also use a switch statement block which results in a jump table instead of logic tests and should be marginally faster. The code below compiles to 5170 bytes, which saves you over 1400 bytes and probably executes a little faster.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long prevMillis = 0;
unsigned long curMillis;
char ns[] = "Next Sunset:";
int i = 0;
void setup()
{
Serial.begin(115200);
lcd.begin(0, 2);
lcd.backlight();
delay(1000);
}
void loop()
{
static unsigned long diff;
char temp[6];
curMillis = millis();
diff = curMillis - prevMillis;
switch (diff) {
case 1000:
lcd.setCursor(18, 2);
lcd.print(strncpy(temp, ns, 2));
break;
case 2000:
lcd.setCursor(17, 2);
lcd.print(strncpy(temp, ns, 3));
break;
case 3000:
lcd.setCursor(16, 2);
lcd.print(strncpy(temp, ns, 4));
break;
case 4000:
lcd.setCursor(15, 2);
lcd.print(strncpy(temp, ns, 5));
break;
case 10000:
lcd.clear();
prevMillis = millis();
break;
}
}